14

I don't think that this could be done in C#, but posting this just to make sure. Here's my problem. I would like to do something like this in C#:

var x = 10;
var l = new List<typeof(x)>();

or

var x = 10;
var t = typeof(x);
var l = new List<t>();

But of course this doesn't work. Although this shouldn't be a problem as the type t is resolved at compile time. I understand that this can be solved with reflection, but as types are known at compile time using reflection would be overkill.

2
  • Is x always going to be 10 or is that just a proof of concept? I'm just wondering if you can avoid using the var keyword. Commented Aug 31, 2009 at 17:54
  • It's just a proof of a concept. I was trying to use this kind of code for Func<> and Expression<> to get rid of redundant typing. So the type could be declared explicitly: <pre><code> int x = 10; var l = new List(); </code></pre> Commented Aug 31, 2009 at 18:15

4 Answers 4

41
public static List<T> CreateCompatibleList<T> (T t)
{
    return new List<T> () ;
}

var x = 10 ;
var l = CreateCompatibleList (x) ;
Sign up to request clarification or add additional context in comments.

3 Comments

Nice one. Really like this solution.
That's it. Great! Thanks. Although it seems that this is something C# team should work on.
One thing you could add to this is to make it an extension method. Then you could use syntax like this: var l = x.CreateList()
6

You're trying to get .Net to set a generic type using a run-time operator. As you know, that won't work. Generics types must be set at compile time.

Comments

3

Defining generics is done like this in C#:

var someList = new List<int>();

You can not define generics like this:

var x = 1;
var someType = typeof(x);
var someList = new List<someType>();

because you're specifying the type at run time in this case, not compile time. C# doesn't support that.

6 Comments

You just repeated what Max stated in the question.
@Anton No I didn't, Max is under the impression that typeof(x) is resolved at compile time, which is not correct.
@Joseph: Are you sure? I know that x.GetType() is resolved at runtime, because it gives the type that x is currently refering to, but I thought typeof(x) will always give the type of the variable, as defined at compile time.
In the case of the sample code, the type of someType is System.Type, so it works, but not as desired. You get a list of types, not a list of ints.
Yes, I know how to define generics. What I was trying to do is to get rid of redundancy in type declarations. I'm not specifying types at runtime. Type inference is resolved at compile time and typeof() operator is also resolves at compile time. It's the GetType() method that resolves type at run time.
|
2

You can't do it exactly how you are asking, but with a little reflection you can accomplish the same thing

Type genericType = typeof(List<>);
Type[] type = new Type[] { typeof(int) };
Type instanceType = genericType.MakeGenericType(type);
object instance = Activator.CreateInstance(instanceType );
var l = (List<int>)instance;

1 Comment

(List<string>) in the last line defeats your purpose. But yes, this is useful in certain cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.