Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this is not possible in C# Generics?

Tags:

java

c#

generics

A colleague pointed me to a strange case in C# (not so sure if this actually strange though).
Suppose you have a class Employee. If you want to create a Generic List<> of type Employee, you can simply do:

List<Employee> x = new List<Employee>;

I understand that I need to pass the Employee type to the Generic list so that it knows the required type information about Employee and generates methods that return and accept parameters that are compatible with Employee.

Now my question is, why isn't it possible to do the following?

Employee x = new Employee();
List<typeof(x)> list = new List<typeof(x)>();

Shouldn't this suffice the information required for List<> to know, in order to create a list? In other words, the type of x which is the type of Employee is now passed as a generic type parameter to List<>, which (as I used to believe) is the same as passing list the type name (in this case Employee).

I know that something like this is available in Java (using the .class) keyword on a variable.

I'm sure I AM missing something, so please, enlight me guys!

like image 309
Galilyou Avatar asked Feb 03 '10 15:02

Galilyou


People also ask

What is not possible in C?

Explanation: Function Overloading is not possible in C as it is not an Object-Oriented Language.

Is comparison of function pointer possible in C?

Comparing a function pointer to a value that is not a null function pointer of the same type will be diagnosed because it typically indicates programmer error and can result in unexpected behavior.

Which of the following is not possible in C programming without using specialized library?

Returning a function pointer, Array of function pointer and Comparison of function pointer aren't possible in c.

What is in C++ but not in C?

C++ was developed by Bjarne Stroustrup in 1979. C does no support polymorphism, encapsulation, and inheritance which means that C does not support object oriented programming. C++ supports polymorphism, encapsulation, and inheritance because it is an object oriented programming language.


2 Answers

No, the equivalent of that isn't available in Java. You can't use "x.class" to get at the declared type of a variable.

Moreover, typeof(x) doesn't work in C# either to get the type of a variable - it returns a Type reference for the type name, e.g. typeof(string) will return a reference to the Type object associated with the System.String type. That's equivalent to using String.class in Java. (Note that again, that's applying .class to a type name, not a variable name.)

Java generics don't support anything like your final statement either. If you believe they do, please give a sample :)

What you can do in C# is use type inference to do what you want:

public static List<T> CreateListForSampleType<T>(T sample)
{
    return new List<T>();
}

...

Employee x = new Employee();
var list = CreateListForSampleType(x);

Note that there's no reason why C# couldn't be extended to allow something like typeof(variablename) or List<typeof(variablename)> - it's all compile-time type information, after all. However, I can't see that it would meet the team's requirements for usefulness... there are other far more useful features I'd like to see first :)

like image 94
Jon Skeet Avatar answered Sep 28 '22 12:09

Jon Skeet


The reason for this is that typeof() returns a type object, while you need a type name to initialize a list at compile-time.

like image 22
Håvard S Avatar answered Sep 28 '22 12:09

Håvard S