java - Why this is not possible in C# Generics? -


A colleague told me a strange case in C # (though this is not really strange)
You have a class employee if you want to create a general list, you can do this:

  list & lt; Employee & gt; X = new list & lt; Employee & gt ;;  

I understand that I need to pass the type of employee in the general list so that I can know the essential types of information about the employee and generate those methods which are relevant to the employees Return and accept the parameters.

Now my question is, why is not it possible to do the following?

  employee x = new employee (); & Lt; Typeof (x) & gt; List = new list & lt; Typef (x) & gt; ();  

The necessary information for the list should not be sufficient to know, to create the list? In other words, the type of X that is the type of employee has now been passed as a generic type parameter to list, (as I believe) the same type of list (in this case the employee).

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

I'm sure I'm missing something, please, please, let me highlight the people!

No, not is available equal to Java "x.class" Can not use to achieve the declared type of variable.

In addition, Type (x) does not work in C # type of a variable - this type name, for example Type returns typef (string) type will return a reference to type System.String . This is equivalent to using String.class in Java. (Note that again, this is going to apply a type of name to .class , not a variable name.)

Java Generic If you do not support anything like the final statement, if you think they do this, please give a sample :)

You can do in C # What do you want to do:

  public static list & lt; T & gt; CreateListForSampleType & lt; T & gt; (T sample) {Return new list & lt; T & gt; (); } ... employee x = new employee (); Var list = CreateListForSampleType (x);  

Note that to allow something like typeof (variablename) or list & lt, C # not extended Can not be done; Typef (Wellbidden) & gt; - It's all compilation time type information, however, I can not see that it will meet the team's requirements for utility ... I still have many other useful features that I want to see first: )


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -