// Compiler error!
CarCollection myInts = new CarCollection();
Do be aware that generic methods can also leverage the where keyword. For example, if you
wish to ensure that only System.ValueType-derived types are passed into the Swap() method created
previously in this chapter, update the code accordingly:
// This method will swap any value type, but not classes.
static void Swap(ref T a, ref T b) where T : struct
{
...
}
Understand that if you were to constrain the Swap() method in this manner, you would no
longer be able to swap string types (as done in the sample code) as they are reference types.
Do'stlaringiz bilan baham: |