Linked Questions
13 questions linked to/from Why must I provide explicitly generic parameter types While the compiler should infer the type?
20 votes
3 answers
9k views
Generics can't infer second parameter? [duplicate]
I've noticed that the C# compiler doesn't infer second generic parameter. Example: C++ template code: (yea I know that templates don't work like generics) class Test { public: template <class T,...
5 votes
1 answer
2k views
Extension Method With Generic Parameter To Generic Class [duplicate]
The code below does not compile: public static class MyExtensions { public static Bar<M> Convert<T,M>(this Foo<T> list) { return new Bar<M>(); ...
0 votes
1 answer
270 views
c# Implicit Generic Types with 2 Keys [duplicate]
I am writing a generic repository for C# and Entity Framework and I am trying to get this code to work without adding a whole bunch of extra interfaces. Essentially, I have a class called ...
3 votes
0 answers
190 views
Generic type parameter inference from constraint [duplicate]
I want to define a generic base class for entities within a system that exposes the type of the entity's identifier (e.g. int, Guid, etc.) class Entity<TId> { } class Product : Entity<int&...
1 vote
0 answers
111 views
C# does not infer the second generic type from the parameter [duplicate]
I made a generic Repository class with a GetById method public interface IEntity<T> { public T Id { get; set; } } ... public Product: IEntity<int> { public int Id { get; set; } } .....
0 votes
1 answer
71 views
What are the rules around when generic parameter types must be explicitly specified for extension methods? [duplicate]
With regard to the following extension methods: public static class Extensions { public static void P<T>(this T value) { } public static TResult F<T, TResult>(this T value) { ...
1 vote
0 answers
57 views
Generic constraint where T is a MyClass<U> [duplicate]
I'd like to be able to do this: class IntMyClass : MyClass<int> { } class StringMyClass : MyClass<string> { } class FloatFloatMyClass : MyClass<float, float> { } class Container<T,...
0 votes
0 answers
52 views
C# generics. Two questions [duplicate]
Say i want to do something like this T1 GenericMethod<T1>(T2 arg) where T1 : GenericClass<T2> { ... } seems it isn't possible, i have to write like this T1 GenericMethod<T1, ...
0 votes
0 answers
26 views
Creating generic classes with generic properties [duplicate]
Let's say I have the following classes: public class Class1<T> where T : SubClass1, new() { public T Item { get; protected set; } public Class1() { Item = new T(); ...
0 votes
0 answers
23 views
Retrieving generics from supplied type [duplicate]
Hi I was curious if there's an easy way to retrieve nested generics from a given type. For example say I have: public class Token<T, V> { protected readonly T fieldT; protected readonly ...
1 vote
2 answers
210 views
Advanced Type Inference
I'd like the compiler to infer a type for me, but am unsure if it is possible, or what the best alternative might be. I'd like to do: public static TValue Get<TValue>(TKey key) where TValue : ...
3 votes
3 answers
143 views
Type inference based on calling location
I want to create a wrapper function for a generic class like so: public class ColumnData { public static ColumnData<T> Create<T>(string name, int width, ColumnType type, ...
1 vote
1 answer
399 views
Infer type used in generic which was inherited by other generic
I have a such code: class A<T> { } class B : A<int> { } class C<T1, T2> where T1 : A<T2> { } But when I want to instantiate the C class with B as first generic type I ...