Weak references
Set Hashtable dictionary<k,v>
ArrayList list<t>
classProgram {Static voidMain (string[] args) { //ArrayList list = new ArrayList ()//{ //New Person () {name= "Kobe", age=35},//New Person () {name= "James", age=30},//New Person () {name= "leaf species", age=25},//}; //list. Sort (); //for (int i = 0; i < list. Count; i++)//{ //Person p= list[i] as person; //Console.WriteLine (p.name); //} //Console.readkey (); int[] Nums = {3,5,2,6,9,1 }; for(inti =0; I < Nums. Length; i++) { for(intj = nums. length-1; J > i; j--) { //if (Nums[j]. CompareTo (Nums[j-1]) <0)//{ //int n = nums[j]; //Nums[j]=nums[j-1]; //nums[j-1] = n; //}IComparable com=(IComparable) nums[j]; if(Com.compareto (Nums[j-1]) <0) { intn =Nums[j]; NUMS[J]= Nums[j-1]; Nums[j-1] =N; } } } for(inti =0; I < Nums. Length; i++) {Console.WriteLine (nums[i]); } console.readkey (); } } Public classperson:icomparable { Public stringName {Get; Set; } Public stringEmail {Get; Set; } Public intAge {Get; Set; } Public intCompareTo (Objectobj) {Person P= obj asPerson ; return This. Age-P.age; } }
Packing and unpacking
Value types go directly to the reference type as boxing; The reference type is converted directly to a value type of unboxing.
classProgram {Static voidMain (string[] args) { //int n = 10; //string s = n.tostring ();//not boxing//int m = Int. Parse (s);//It 's not unpacking .//object o = N;//Packing//int x = (int) o;//Unpacking//Console.WriteLine (m); //box unbox appears in the Il language intn =Ten; Objecto =N; intm = (int) O; Console.WriteLine (m); Console.readkey (); } }
classProgram {Static voidMain (string[] args) { //Chinese ch1 = new Chinese (); //Person p = ch1;//Boxing does not occur because it is a reference type//Chinese CH2 = (Chinese) p; //double d = 999.9; //object o = D; //Double D1 = (double) o;//Packing//Console.WriteLine (d1);//unpacking, Output 999.9//Console.readkey (); //int d = 999; //object o = D; //Double D1 = (double) o;//Packing//Console.WriteLine (d1);//An exception occurs when an integer unboxing must be an integral type in boxing//Console.readkey (); //int n = 10; //IComparable com = n;//a boxed interface has occurred that is a reference type//int m = (int) com; //Console.WriteLine (m); //Console.readkey (); intn =Ten; Console.WriteLine (n); Objecto = n;//PackingConsole.WriteLine ((int) o);//UnpackingConsole.WriteLine (o); Console.readkey (); } } Public classPerson { Public stringName {Get; Set; } Public stringEmail {Get; Set; } Public intAge {Get; Set; } } Public classChinese:person {}
classProgram {Static voidMain (string[] args) { intn = -; M1 (n); //call void M1 (double D) does not go boxed stringS1 ="a"; DoubleD1 =Ten; stringS2 ="b"; intN1 =9; stringFull = s1 + d1 + s2 + n1;//boxed two times, D1 and N1Console.WriteLine (full); Console.readkey (); } Private Static voidM1 (Doubled) {Console.WriteLine (d); } Private Static voidM1 (Objecto) {Console.WriteLine (o); } }
classProgram {Static voidMain (string[] args) { intn =Ten; Objecto = n;//Packing oncen = -; Console.WriteLine (n+","+(int) o);//It took two times to dismantle.Console.readkey (); } }
classProgram {Static voidMain (string[] args) { //ArrayList arrilist = new ArrayList (); //Stopwatch watch = new Stopwatch (); //Watch. Start (); //for (int i = 0; i < 1000000; i++)//{ //Arrilist.add (i); //} //Watch. Stop (); //Console.WriteLine (watch. Elapsed);//00:00:00.99816//Console.readkey ();List<int> arrilist =Newlist<int>(); Stopwatch Watch=NewStopwatch (); Watch. Start (); for(inti =0; I <1000000; i++) {arrilist.add (i); } watch. Stop (); Console.WriteLine (watch. Elapsed);//00:00:00.014241Console.readkey (); } }
var type inference: