stack.Push(new Employee() { ID = "456", Name = "Margaret" });
Employee e1 = new Employee() { ID = "123", Name = "John" };
if (stack.Find(e1))
Console.WriteLine("Employee found.");
Specifying Multiple Constraints
You can specify multiple constraints in a generic type. For example, if you want the MyStack
class to manipulate objects of type Employee
and also implement the IComparable
interface, you can declare the generic type as:
public class MyStack where T : Employee, IComparable {
//...
}
Here, you are constraining that the MyStack
class must use types derived from Employee
and they must also implement the IComparable
interface.
The base class constraint must always be specified first, before specifying the interface.
Assuming that you have the following Manager
class deriving from the Employee
class:
public class Manager : Employee, IComparable {
public int CompareTo(Manager obj) {
return base.CompareTo(obj);
}
}
The following statement is now valid:
MyStack stackM = new MyStack(3);
Multiple Type Parameter
So far you have seen only one type parameter used in a generic type, but you can have multiple type parameters. For example, the following MyDictionary
class uses two generic type parameters — K
and V
:
public class MyDictionary {
//...
}
To apply constraints on multiple type parameters, use the where
keyword multiple times:
public class MyDictionary
where K : IComparable where V : ICloneable {
//...
}
Generics can also be applied on interfaces. The following example defines the IMyStack
interface:
interface IMyStack where T : IComparable {
void Push(T item);
T Pop();
bool Find(T keyword);
}
A class implementing a generic interface must supply the same type parameter as well as satisfy the constraints imposed by the interface.
The following shows the generic MyStack
class implementing the generic IMyStack
interface:
public class MyStack : IMyStack
where T : IComparable {
//...
}
Figure 9-4 shows the error reported by Visual Studio 2008 if the generic MyStack
class does not provide the constraint imposed by the generic interface.
Figure 9-4
Generics can also be applied to structs. For example, suppose that you have a Coordinate
struct defined as follows:
public struct Coordinate {
public int x, y, z;
}
The coordinates for the Coordinate
struct takes in int values. You can use generics on the Coordinate
struct, like this:
public struct Coordinate {
public T x, y, z;
}
To use int
values for the Coordinate
struct, you can do so via the following statements:
Coordinate pt1;
pt1.x = 5;
pt1.y = 6;
pt1.z = 7;
To use float
values for the Coordinate
struct, utilize the following statements:
Coordinate pt2;
pt2.x = 2.0F;
pt2.y = 6.3F;
pt2.z = 2.9F;
In addition to generic classes and interfaces, you can also define generic methods. Consider the following class definition and the method contained within it:
public class SomeClass {
public void DoSomething(T t) {}
}
Here, DoSomething()
is a generic method. To use a generic method, you need to provide the type when calling it:
SomeClass sc = new SomeClass();
sc.DoSomething(3);
The C# compiler, however, is smart enough to deduce the type based on the argument passed into the method, so the following statement automatically infers T
to be of type String
:
sc.DoSomething("This is a string"); //---T is String---
This feature is known as generic type inference .
You can also define a constraint for the generic type in a method, like this:
public class SomeClass {
public void DoSomething(T t) where T : IComparable {
}
}
If you need the generic type to be applicable to the entire class, define the type T
at the class level:
public class SomeClass where T : IComparable {
public void DoSomething(T t) { }
}
In this case, you specify the type during the instantiation of SomeClass
:
SomeClass sc = new SomeClass();
sc.DoSomething(3);
You can also use generics on static methods, in addition to instance methods as just described. For example, the earlier DoSomething()
method can be modified to become a static method:
public class SomeClass {
public static void DoSomething(T t)
where T : IComparable {}
}
To call this static generic method, you can either explicitly specify the type or use generic type inference:
SomeClass.DoSomething(3);
//---or---
SomeClass.DoSomething(3);
Generics can also be applied to operators. Consider the generic MyStack
class discussed earlier in this chapter. Suppose that you want to be able to join two MyStack
objects together, like this:
MyStack stack1 = new MyStack(4);
stack1.Push("A");
stack1.Push("B");
MyStack stack2 = new MyStack(2);
stack2.Push("C");
stack2.Push("D");
stack1 += stack2;
In this case, you can overload the +
operator, as highlighted in the following code:
public class MyStack where T : IComparable {
private T[] _elements;
private int _pointer;
public MyStack(int size) {
_elements = new T[size];
_pointer = 0;
}
public void Push(T item) {
if (_pointer < _elements.Length - 1) {
throw new Exception("Stack is full.");
}
_elements[_pointer] = item;
_pointer++;
}
public T Pop() {
_pointer--;
if (_pointer < 0) {
return default(T);
}
return _elements[_pointer];
}
public bool Find(T keyword) {
bool found = false;
Читать дальше