3

I'm trying to create a generic list using C# and .NET 4.0

List<var> varlist = new List<var>(); This code produces the error "The contextual keyword 'var' may only appear in a local variable declaration"

The reason being is because I want to do something like this:

List<var> varList = new List<var>(); // Declare the generic list
List<Object1> listObj1;              // List of Object1
List<Object2> listObj2;              // List of Object2

varList = listObj1;                  // Assign the var list to list of Object1

varList = listObj2;                  // Re-assign the var list to the list of Object2

Is there any way to do this in C# .NET 4.0?

11
  • use object instead of var Commented Apr 28, 2014 at 15:42
  • When you have a general reference to your list, what do you want to do with it? A List<T> has a few base interfaces that you can reference to them as, some are not generic so will allow you to not care about the type, but then you probably don't want to do type-specific things with it. Commented Apr 28, 2014 at 15:42
  • Is there any way to do this then? Not using var? Commented Apr 28, 2014 at 15:43
  • List<var> wouldn't be just generic, it would be super-generic. var's type must be resolvable upon the declaration of the variable. Commented Apr 28, 2014 at 15:43
  • 1
    Your idea of a generic container list, where you would assign completely different and unrelated lists seems, as written below, a List<T> job Commented Apr 28, 2014 at 15:50

3 Answers 3

9

If you want a generic list, then you have to define that generic type somewhere. I'm assuming this is at classlevel:

class MyClass<T> {
    List<T> someGenericList = new List<T>();
}

Or you use the baseclass for all types:

List<object> someGenericList = new List<object>();

Or you make it dynamic (only use this when you're absolutely sure you need it)

List<dynamic> someGenericList = new List<dynamic>();

Or lastly: you adapt your design. Typically if you have to define such a high level type of collection, you have options to refactor it in a way that it allows you to use more of the common type. If you want feedback on this then you would have to give more information concerning the context.

Don't forget to read MSDN: An introduction to C# generics.

The reason your code doesn't work is because var is a keyword and not a type. It substitutes itself for the actual type of the expression that follows and thus allows you to get right to creating your expression instead of having to explicitly define it first. Therefore in order to use it, there actually has to be an expression that follows it.

Sign up to request clarification or add additional context in comments.

2 Comments

And how does this different from what I wrote, meanwhile my answer get down voted for no apparent reason...
A generic list is not the same as a list of type object. That's just a really vague list.
1

You cannot define method parameters with var because the var part must be resolved at compile-time and the compiler cannot do that. Your best bet is to use:

List<dynamic> dynamicList = new List<dynamic>();

which, I think, should be resolved during compile time to its equivalent

List<object> dynamicList = new List<object>();

Then, every time you are accessing the members of the list, in order to use them to do something meaningful, you will have to explicitly cast them to your required type. You have to consider whether it is worth to do so. It would be better if you can either:

  1. Use different lists for different types
  2. Use a base class with a relevant abstract or virtual method that is being overriden with the expected behavior.

1 Comment

Unless you intend to explain how to use this, it won't work if we take the OP code verbatim and change it to use this.
0

Change var to something which is not keyword in C#. For example

 List<object> varlist = new List<object>();

2 Comments

@PatrickHofman read Jeroen Vannevel answer and explain how it has nothing to do with it... And we post our answers at same time by the way.
It has nothing to do with var being a keyword. dynamic is a keyword too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.