nested-generics

What do nested generics in C# mean?

杀马特。学长 韩版系。学妹 提交于 2021-02-16 17:00:55
问题 A bit of a basic question, but one that seems to stump me, nonetheless. Given a "nested generic": IEnumerable<KeyValuePair<TKey, TValue>> Is this stating that IEnumerable can have generic types that are themselves KeyValuePair 's ? Thanks, Scott 回答1: Yes. The KeyValuePair type expects two generic type parameters. We can either populate them by pointing to concrete types: IEnumerable<KeyValuePair<string, int>> Or we can populate them by using other generic parameters already specified by the

C# foreach on IEnumerable<IList<object>> compiles but shouldn't

老子叫甜甜 提交于 2020-01-02 02:34:29
问题 I have the following code: IEnumerable<IList<MyClass>> myData = //...getMyData foreach (MyClass o in myData) { // do something } It compiles, runs and obviously I get an System.InvalidCastException . Why does the compiler not complain? MyClass is a simple bean, no extensions. Edit 1: As suggested by David switching the type from IList to List the compiler complains Edit 2: I've understood that the behaviour is as specified in the C# Language definition. However, I don't understand why such a

Generics C# organization of methods that depends on type [closed]

你离开我真会死。 提交于 2019-12-14 03:33:35
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I'm Tryng to made a base class with a base method that order a List with a function that depends on Type. My Compiler show this Error Error 13 Impossibile to convert 'System.Linq.Expressions.Expression<System.Func<MLOLPlus.Business.Dealer,string>>' in 'System.Linq.Expressions.Expression<System.Func<T,string>>'.

How to keep generic type of nested generics with class tokens

心不动则不痛 提交于 2019-12-12 03:28:00
问题 The standard way in Java to work around type erasure is to pass a class token into the constructor. For example we could define a generic property class like this: class Prop<T> { public Prop(Class<T> type) { this.type = type; } Class<T> type; T t; } class IntProp extends Prop<Integer> { public IntProp() { super(Integer.class); } } But what if I now want to use another generic type argument, such as a list and also keep its generics type. I would have liked to do this: class ListProp<J>

Nested Type Parameters in Java

China☆狼群 提交于 2019-12-09 05:26:15
问题 This is an example which I made up to be a simplification of my real code, so I apologize if it is a little contrived. What I would like to do is to effectively get two type parameters out of a single nested type argument. I'm pretty sure this is impossible, but I thought I'd give it a shot. //Not legal java code public class Foo<C extends Collection<T>> { //where T is another type parameter private C coll; public Foo(C coll) { this.coll = coll; } public void add(T elem){ this.coll.add(elem);

Generic tree, self bounded-generics

跟風遠走 提交于 2019-12-07 08:19:09
问题 I am to adding genericity to one of my projects. I love generics as this makes my code more robust, self-documented and erases all those ugly casts. However, I came across a tough case and have some issues trying to express a "recursive" constraint for one of my structures. This is basically some kind of "generic" tree, with double links (to children and parent). I have simplified the class at maximum to show the issue : public class GenericTree< ParentClass extends GenericTree<?, ?>,

Generic tree, self bounded-generics

心不动则不痛 提交于 2019-12-05 16:12:05
I am to adding genericity to one of my projects. I love generics as this makes my code more robust, self-documented and erases all those ugly casts. However, I came across a tough case and have some issues trying to express a "recursive" constraint for one of my structures. This is basically some kind of "generic" tree, with double links (to children and parent). I have simplified the class at maximum to show the issue : public class GenericTree< ParentClass extends GenericTree<?, ?>, ChildClass extends GenericTree<?, ?>> { // Attributes private ArrayList<ChildClass> children = new ArrayList

Flatten IEnumerable<IEnumerable<>>; understanding generics

风流意气都作罢 提交于 2019-12-05 08:28:30
问题 I wrote this extension method (which compiles): public static IEnumerable<J> Flatten<T, J>(this IEnumerable<T> @this) where T : IEnumerable<J> { foreach (T t in @this) foreach (J j in t) yield return j; } The code below causes a compile time error (no suitable method found), why? : IEnumerable<IEnumerable<int>> foo = new int[2][]; var bar = foo.Flatten(); If I implement the extension like below, I get no compile time error: public static IEnumerable<J> Flatten<J>(this IEnumerable<IEnumerable

C# foreach on IEnumerable<IList<object>> compiles but shouldn't

对着背影说爱祢 提交于 2019-12-05 05:30:53
I have the following code: IEnumerable<IList<MyClass>> myData = //...getMyData foreach (MyClass o in myData) { // do something } It compiles, runs and obviously I get an System.InvalidCastException . Why does the compiler not complain? MyClass is a simple bean, no extensions. Edit 1: As suggested by David switching the type from IList to List the compiler complains Edit 2: I've understood that the behaviour is as specified in the C# Language definition . However, I don't understand why such a cast/conversion is allowed, since at runtime I always get an InvalidCastException. I opened this in