Материал из .Net Framework эксперт
Create relationship between two type parameters
using System;
class A {
}
class B : A {
}
// Here, V must inherit T.
class Gen<T, V> where V : T {
}
class MainClass {
public static void Main() {
Gen<A, B> x = new Gen<A, B>();
// error
// Gen<B, A> y = new Gen<B, A>();
}
}
Default generic parameter value
using System;
public class Bag<T>
{
public Bag() {
// Create initial capacity.
imp = new T[ 4 ];
for( int i = 0; i < imp.Length; ++i ) {
imp[i] = default(T);
}
}
public bool IsNull( int i ) {
if( i < 0 || i >= imp.Length ) {
throw new ArgumentOutOfRangeException();
}
if( Object.Equals(imp[i], default(T)) ) {
return true;
} else {
return false;
}
}
private T[] imp;
}
public class MainClass
{
static void Main() {
Bag<int> intBag = new Bag<int>();
Bag<object> objBag = new Bag<object>();
Console.WriteLine( intBag.IsNull(0) );
Console.WriteLine( objBag.IsNull(0) );
}
}
True
True