is there such thing as a user defined function that accepts type identifiers?

This is a discussion on is there such thing as a user defined function that accepts type identifiers? within the C# Programming forums, part of the General Programming Boards category; methods that have parameters can be called by passing a variable identifier or an expression as an argument. and then ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    71

    is there such thing as a user defined function that accepts type identifiers?

    methods that have parameters can be called by passing a variable identifier or an expression as an argument.

    and then there are built-in 'methods' such as typeof(<type identifier>) which returns a System.Type object, and default(<type identifier>) which returns a null reference for reference types or the default instance of a value type; and what's interesting about them is that unlike traditional methods they aren't called by passing a variable identifier or expression but by passing a type identifier.

    is it possible in C# to create 'methods' that work in a similar manner?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    I think you're looking for generic methods. You can read about them here: Generic Methods.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    71
    Quote Originally Posted by itsme86 View Post
    I think you're looking for generic methods. You can read about them here: Generic Methods.
    well it's true that when you call a generic method you need to pass a type argument (at least in cases where the compiler cannot infer the type parameters from the method arguments) but a generic method is not what I have in mind, since, ultimately, inside the generic method, just like in any other method, you still get to work with a variable identifier that represents a variable of *some type*, as opposed to the actual type identifier that represents the type itself.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Well what exactly are you trying to do? Maybe reflection can help you. It's hard to tell with your abstract question what can help you reach your mystery goal.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    71
    I'm trying to do something along the following lines without having to manually type the type identifiers:

    Code:
    Console.WriteLine(typeof(ushort) + ", bits: " + sizeof(ushort) * 8);
    Console.WriteLine(typeof(int) + ", bits: " + sizeof(int) * 8);
    Console.WriteLine(typeof(uint) + ", bits: " + sizeof(uint) * 8);
    Console.WriteLine(typeof(long) + ", bits: " + sizeof(long) * 8);
    Console.WriteLine(typeof(ulong) + ", bits: " + sizeof(ulong) * 8);
    Console.WriteLine(typeof(float) + ", bits: " + sizeof(float) * 8);
    Console.WriteLine(typeof(double) + ", bits: " + sizeof(double) * 8);
    Console.WriteLine(typeof(decimal) + ", bits: " + sizeof(decimal) * 8);
    Console.WriteLine(typeof(IntPtr) + ", bits: " + sizeof(IntPtr) * 8);
    Console.WriteLine(typeof(UIntPtr) + ", bits: " + sizeof(UIntPtr) * 8);
    Console.WriteLine(typeof(DateTime) + ", bits: " + sizeof(DateTime) * 8);
    Console.WriteLine(typeof(ConsoleColor) + ", bits: " + sizeof(ConsoleColor) * 8);
    Console.WriteLine(typeof(TimeSpan) + ", bits: " + sizeof(TimeSpan) * 8);
    true, instead of relying on the typeof operator I can use the GetType() method, but I'm still left with no known way of calling the sizeof operator.

    I know that I could, for example, create an instance of the System.Reflection.Assembly class (by assigned a reference to an instance of a derived type of course) and call the GetTypes() method on it, select desired types (ie: value types) then loop through the System.Type[] array object and print relevant info. However, I'm still unsure there is a proper substitute for the sizeof operator, which in addition to working only in an unsafe context, it works only with some value types (ie: enums, built-in value types, user-defined structs as long as they do not have fields of managed types.)

    A nice alternative would be to define a generic method that infers the type parameter based on the method argument (ie: Print<T>(T t){..}) which can then be called with only method argument and no type argument, but the sizeof operator sort of messes things up because it doesn't work with all types. adding a generic constraint to the Print<T> method seems like it should work but even "where T : struct" will cause problems because the sizeof operator will not work with all value types.

    Who knows, maybe System.Reflection offers a runtime alternative to the sizeof operator.

    Any thoughts?
    Last edited by y99q; 11-28-2011 at 11:34 PM.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    System.Runtime.InteropServices.Marshal.SizeOf
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User defined cos function
    By zfite in forum C Programming
    Replies: 11
    Last Post: 04-03-2011, 02:40 AM
  2. how to convert char string to user defined data type
    By prdeepss in forum C Programming
    Replies: 5
    Last Post: 08-02-2009, 09:59 AM
  3. user defined operator returns another type?
    By TriKri in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2007, 02:47 AM
  4. Determining a user-defined class type
    By Mingzhi in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2004, 05:33 PM
  5. Static Capabilities for a User-Defined Type
    By gozlan in forum C# Programming
    Replies: 1
    Last Post: 04-08-2003, 11:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21