1. The generic range is more house, better.
2.readonly is read-only meaning that it cannot be written.
3.return default (t) default (t) in generics if you do not limit the T type argument to a value type or reference type, you may get an error inside your program,
Because the value type does not allow NULL. So default is used to get a type of defaults, and for a value type get new T () basically get 0;
will get null for reference type
Or if you don't use the default keyword, you get by reflection whether T refers to a type or a reference type and then sets the default value.
4.public class Cage <t>{
T[] Array;
readonly int Size;
int num;//is already a few
public Cage (int n) {
Size = n;
num = 0;
Array = new T[size];
}
public void Putin (T pet) {
if (num <size) {
Array [num++] = pet;
}else{
Debug.Log ("Cage is full");
}
}
Public T Takeout () {
if (num>0) {
return array [--num];
}else {
Debug.Log ("Cage is empty");
return default (T);
}
}
}
var dogcage = new cage<dog> (1);
Dogcage.putin (New Dog ("A"));
Dogcage.putin (New Dog ("B"));
var dog = Dogcage.takeout ();
Dog. Printname ();
5. Generic Methods
void dogishappy<t> (T target) {
}
public void ishappy<t> (T target) {
Debug.Log ("HappY:" + target.) ToString ());
}
var dog = new Dog ("a");
Dog. Ishappy<int> (3);
C # Object-oriented 5