C # Object-oriented 5

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.