public class MyStack {
private int[] _elements;
private int _pointer;
public MyStack(int size) {
_elements = new int[size];
_pointer = 0;
}
public void Push(int item) {
if (_pointer > _elements.Length - 1) {
throw new Exception("Stack is full.");
}
_elements[_pointer] = item;
_pointer++;
}
public int Pop() {
_pointer--;
if (_pointer < 0) {
throw new Exception("Stack is empty.");
}
return _elements[_pointer];
}
}
In this case, the MyStack
class allows data of int type to be pushed into and popped out of the stack. The following statements show how to use the MyStack
class:
MyStack stack = new MyStack(3);
stack.Push(1);
stack.Push(2);
stack.Push(3);
Console.WriteLine(stack.Pop()); //---3---
Console.WriteLine(stack.Pop()); //---2---
Console.WriteLine(stack.Pop()); //---1---
As you can see, this stack implementation accepts stack items of the int
data type. To use this implementation for another data type, say String
, you need to create another class that uses the string
type. Obviously, this is not a very efficient way of writing your class definitions because you now have several versions of essentially the same class to maintain.
A common way of solving this problem is to use the Object
data type so that the compiler will use late-binding during runtime:
public class MyStack {
private object[] _elements;
private int _pointer;
public MyStack(int size) {
_elements = new object[size];
_pointer = 0;
}
public void Push(object item) {
if (_pointer > _elements.Length - 1) {
throw new Exception("Stack is full.");
}
_elements[_pointer] = item;
_pointer++;
}
public object Pop() {
_pointer-- ;
if (_pointer < 0) {
throw new Exception("Stack is empty.";
}
return _elements[_pointer];
}
}
One problem with this approach is that when you use the stack class, you may inadvertently pop out the wrong data type, as shown in the following highlighted code:
MyStack stack = new MyStack(3);
stack.Push(1);
stack.Push(2);
stack.Push("A");
//---invalid cast---
int num = (int)stack.Pop();
Because the Pop()
method returns a variable of Object
type, IntelliSense cannot detect during design time if this code is correct. It is only during runtime that when you try to pop out a string
type and try to typecast it into an int type that an error occurs. Besides, type casting (boxing and unboxing) during runtime incurs a performance penalty.
To resolve this inflexibility, you can make use of generics.
Using generics, you do not need to fix the data type of the items used by your stack class. Instead, you use a generic type parameter ( ) that identifies the data type parameter on a class, structure, interface, delegate, or procedure. Here's a rewrite of the MyStack
class that shows the use of generics:
public class MyStack {
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) {
throw new Exception("Stack is empty.");
}
return _elements[_pointer];
}
}
As highlighted, you use the type T
as a placeholder for the eventual data type that you want to use for the class. In other words, during the design stage of this class, you do not specify the actual data type that the MyStack
class will deal with. The MyStack
class is now known as a generic type.
When declaring the private member array _element
, you use the generic parameter T
instead of a specific type such as int
or string
:
private T[] _elements;
In short, you replace all specific data types with the generic parameter T
.
You can use any variable name you want to represent the generic parameter. T
is chosen as the generic parameter for illustration purposes.
If you want the MyStack
class to manipulate items of type int
, specify that during the instantiation stage ( int
is called the type argument):
MyStack stack = new MyStack(3);
The stack object is now known as a constructed type, and you can use the MyStack
class normally:
stack.Push(1);
stack.Push(2);
stack.Push(3);
A constructed type is a generic type with at least one type argument.
In Figure 9-1 IntelliSense shows that the Push()
method now accepts arguments of type int
.
Figure 9-1
Trying to push a string value into the stack like this:
stack.Push("A"); //---Error---
generates a compile-time error. That's because the compiler checks the data type used by the MyStack
class during compile time. This is one of the key advantages of using generics in C#.
To use the MyStack
class for String
data types, you simply do this:
MyStack stack = new MyStack(3);
stack.Push("A");
stack.Push("B");
stack.Push("C");
Figure 9-2 summarizes the terms used in a generic type.
Figure 9-2
Using the default Keyword in Generics
In the preceding implementation of the generic MyStack
class, the Pop()
method throws an exception whenever you call it when the stack is empty:
public T Pop() {
_pointer--;
if (_pointer < 0) {
throw new Exception("Stack is empty.");
}
return _elements[_pointer];
}
Rather than throwing an exception, you might want to return the default value of the type used in the class. If the stack is dealing with int
values, it should return 0; if the stack is dealing with string
, it should return an empty string. In this case, you can use the default keyword to return the default value of a type:
Читать дальше