.net - Generic list in C# 4.0 -
I am trying to create a general list using C # and .NET 4.0
list & lt; On & gt; Varlist = new list & lt; Var & gt; ();
This code generates an error "The relevant keyword 'var' may only appear in a local variable declaration"
The reason for this is that I want to do something like this:
list & lt; Var & gt; VarList = new list & lt; Var & gt; (); // Announce generic list & lt; Object1 & gt; ListObj1; List of object 1 list & amp; Object 2 & gt; ListObj2; // List of object 2 varList = listObj1; // Assign the var list to list in the list of object 1 varList = listObj2; // Assign the var list to the list of object 2
Is there any way to do this in C # .NET 4.0?
If you want a general list, then you have to define that normal type somewhere I'm assuming that this is on classwell:
class MyClass & lt; T & gt; {List & lt; T & gt; SomeGenericList = new list & lt; T & gt; (); }
Or you use all kinds of base classes:
list & lt; Object & gt; SomeGenericList = new list & lt; Object & gt; ();
Or you can make it dynamic
(use it only when you're absolutely sure you need it)
list & gt; Dynamic & gt; SomeGenericList = New List & gt; Dynamic & gt; ();
Or at the end: You adapt your design. Generally if you have to define such a high level collection, then you have the option to rearrange it in such a way that you can use more than usual. If you want a response to this, then you have to give more information related to the context.
Do not forget to read.
Your code does not work, because the reason is that it does not have a keyword and no type substitutes for the actual type of expression of its own, and thus you have to explicitly define it first Allows you to acquire the right to make your expression. Therefore, to use it, it should actually be an expression that follows it.
Comments
Post a Comment