Say you have a general class Foo:
public class Foo<T> {
public T Data {
get;
protected set;
}
}
Is it possible to define a constructor that is applicable only if it Tinherits (or is) a specific type.
For example, Tthis is int:
public Foo () {
this.Data = 42;
}
Type restriction must be checked at compile time. This may be useful for optimization. Say, for example, you have IEnumerable<T>one and you want to create a "cache" (since LINQ queries can be quite expensive). Now, if it is IEnumerable<T>already IList<T>, it is useful not to copy the data. On the other hand, if it is really a LINQ query, another constructor can store the data in an array.
, , Foo (, IntFoo) :
public class IntFoo : Foo<int> {
public IntFoo () {
this.Data = 42;
}
}
, private ( protected). , , , ?