generic-constraints

How to define an aggregated ICollection<T> where T is type of the current declaring class within a hierarchy?

拜拜、爱过 提交于 2021-02-19 12:56:19
问题 I need to inherit a collection of items of the current type, like this class A { // some properties... public ICollection<A> Children; } class B: A { // other properties } This mostly works as expected. The problem is I can do something like this class C: A { } B b = new B(); b.Children = new List<C>(); Is there any way to force b.Children to be a collection of B ? 回答1: No, there is no way to do such thing yet. The C# language has no artifact to declare such thing: class A { public

How to define an aggregated ICollection<T> where T is type of the current declaring class within a hierarchy?

纵饮孤独 提交于 2021-02-19 12:51:46
问题 I need to inherit a collection of items of the current type, like this class A { // some properties... public ICollection<A> Children; } class B: A { // other properties } This mostly works as expected. The problem is I can do something like this class C: A { } B b = new B(); b.Children = new List<C>(); Is there any way to force b.Children to be a collection of B ? 回答1: No, there is no way to do such thing yet. The C# language has no artifact to declare such thing: class A { public

How to define an aggregated ICollection<T> where T is type of the current declaring class within a hierarchy?

别说谁变了你拦得住时间么 提交于 2021-02-19 12:48:31
问题 I need to inherit a collection of items of the current type, like this class A { // some properties... public ICollection<A> Children; } class B: A { // other properties } This mostly works as expected. The problem is I can do something like this class C: A { } B b = new B(); b.Children = new List<C>(); Is there any way to force b.Children to be a collection of B ? 回答1: No, there is no way to do such thing yet. The C# language has no artifact to declare such thing: class A { public

Base class constraint on generic class specifying the class itself

给你一囗甜甜゛ 提交于 2020-07-15 11:13:04
问题 Yesterday, I was explaining C#'s generic constraints to my friends. When demonstrating the where T : CLASSNAME constraint, I whipped up something like this: public class UnusableClass<T> where T : UnusableClass<T> { public static int method(T input){ return 0; } } And was really surprised to see it compile. After a bit of thinking, however, I figured it was perfectly legal from the point of view of the compiler - UnusableClass<T> is as much of a class as any other that can be used in this

Cannot pass variable of type conforming to generic constraint

蹲街弑〆低调 提交于 2020-01-04 02:53:09
问题 public interface ILovable<T> where T : IEquatable<T> { T Care(T t); } public class Me : ILovable<int> { public int Care(int i) { return i; } } Say I have the above. Now below function fails: private static void Colour<T>(ILovable<T> me) where T : IEquatable<T> { var z = me.Care(1); //cannot convert from 'int' to 'T' } What's failing the above piece of code? ILovable<T> has a Care function which intakes a T which is IEquatable<T> . In the above function I'm calling the same Care function and

Why does Nullable<T> not match as a reference type for generic constraints [duplicate]

放肆的年华 提交于 2020-01-01 07:36:05
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Nullable type as a generic parameter possible? I came across a very weird thing with generic type constraints. I have a class like this: public SomeClass<T> where T:class { } However, I've found I can't use nullable types as I'd expect: new SomeClass<int?>(); I get an error that int? must be a reference type. Is Nullable really just a struct with syntactic sugar to make it look like a reference type? 回答1:

Member with the same signature already defined with different type constraints

跟風遠走 提交于 2019-12-19 07:18:23
问题 I have run across an issue with overloading methods that have different constraints that seems exclusive. That is my example: public class A { public void Do<T>() where T : class { } public void Do<T>() where T : struct { } } And this does not compile with the following error "Member with the same signature already defined". Is it possible to satisfy both conditions at once or it's just the limitation of the C# compiler? 回答1: It's not a limitation of the compiler - it's a limitation of the