This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why can't the C# constructor infer type?
Why is the following true:
var foo = new KeyValuePair(3,4); //doesn't compile!
var boo = new KeyValuePair<int,int>(3,4); //works fine!
I would think both lines would be legal, since the type can be (should be) inferred from the parameters. Explanation?
Simply put, type inference only works on methods, not constructors. The reason for this is simple, constructors do not take type arguments, only types and methods do. To wit, KeyValuePair is an undefined type. Remember, it is possible, for example, to have the following types: Action, Action<T>, Action<T1, T2>, etc.
Related
This question already has answers here:
Simplify generic type inferring
(3 answers)
Why doesn't C# infer my generic types?
(9 answers)
C# Generic Type Inference With Multiple Types
(2 answers)
C# generic method type argument not inferred from usage
(3 answers)
Closed 1 year ago.
This is a purely aesthetic thing, but I think it is worth asking nonetheless. In the following code:
interface IGiveResult<TResult>
{
TResult GetResult();
}
static class ResultGetter
{
GetResultFrom<TResultGiver, TResult>(TResultGiver giver) where TResultGiver : IGiveResult<TResult>
{
return giver.GetResult();
}
}
To call GetResultFrom, I need to specify both type parameters like this:
ResultGetter.GetResultFrom<SomeGiverType, SomeResultType>(someGiverInstance);
However, I would like to call is like this:
ResultGetter.GetResultFrom<SomeGiverType>(someGiverInstance);
After all, the compiler has all the information neccessary to infer the second generic type. However, this does not compile. Is it possible to change this code, so that one doesn't have to specify the result type?
This question already has answers here:
How do I use reflection to call a generic method?
(8 answers)
Closed 2 years ago.
Why can't I do this?
services.AddSingleton<Type.GetType("ShoppingCartCache",true)>();
OR is there a better way of passing from a string
The method also has an overload to pass a type as parameter. So you can do:
services.AddSingleton(typeof(ShoppingCartCache));
Same also works with your example (although more prone to runtime errors):
services.AddSingleton(Type.GetType("ShoppingCartCache",true));
The reason it doesn't work is because generic types must be static, thus known at compilation.
This question already has answers here:
C# Language: How to get type of bound but open Generic class?
(4 answers)
Closed 6 years ago.
I am wondering if anyone knows a way to get a representation of a partially open generic type in C#, for example IDictionary<string,>.
What I have tried:
typeof(IDictionary<string,>)
I get: Partially opened type is not permitted in 'typeof' expression compile error.
typeof(IDictionary<,>).MakeGenericType(typeof(string))
I get: ArgumentException (The number of generic arguments provided doesn't equal the arity of the generic type definition. Parameter name: instantiation)
Why I want to do this:
I have a number of scenarios where I need to check if a class I have implements an interface, but in several cases I know that some of the generic type parameters have to be specific (i.e. I want something that implements a dictionary with string keys but I don't care about the value type). I realize there are a number of other ways I could do this (for example, by providing an array of necessary generic parameter types to my method). But in looking at the problem I got curious if there is a way to specify partially open / partially closed generic types, hence the question.
Maybe something like that could answer to your problem:
class CustomDictionary<TValue> : Dictionary<string, TValue>
{
}
And this should work:
typeof(CustomDictionary<>).MakeGenericType(typeof(string))
This question already has answers here:
Why covariance and contravariance do not support value type
(4 answers)
Closed 7 years ago.
Why the compiler doesn't allow to assign ints to objects?
IQueryable<object> objects = null;
IQueryable<int> ints = null;
objects = ints;
Implicit type conversion (a function of covariance) does not apply to all generics. SomeGeneric<ValueType> is not derived from to SomeGeneric<Reference> and thus, it is not valid to cast it even if there is a already an implicit conversion for the type parameters (in this case, boxing).
If you are in C# 4.0, a generic interface can be defined as covariant using ISomeGeneric<out T> and provided that the generic arguments are derived from one another, then you can cast. If the generic arguments are not derived, it is not possible to cast.
This question already has answers here:
Generating Delegate Types dynamically in C#
(2 answers)
Closed 8 years ago.
In C# can you do something like
Func<typeof(variableType),int)> myDelegate;
where you can pass the type arguments dynamically to a delegate?
No. typeof() is evaluated at run-time. Your delegate declaration is evaluated at compile time. The typeof() evaluation would have to occur first for this to work.
You can not use Func<typeof(variableType),int)> myDelegate;.and get syntax error.
Use:
Func<object,int> myDelegate;
or:
Func<dynamic,int> myDelegate;
And see this:Generating Delegate Types dynamically in C#