- Value types and reference types
Value type: int double char bool decimal struct enum (inherited from System.ValueType)
Reference type: String Array class interface delegate
The value type exists on the stack of memory, the reference type is stored in the heap, and the stack is more efficient than the heap
Value passing: The value is passed as a parameter and the value itself is passed. Ref can pass a value to a reference pass
Reference passing: A reference is passed.
1 Private voidButton1_Click (Objectsender, EventArgs e)2 {3Person p =NewPerson ();4P.name ="Zhang San";5 test (p);6 MessageBox.Show (p.name);7 //John Doe8 }9 Ten One A voidTest (person s) - { -S.name ="John Doe"; theperson P2 =NewPerson (); -P2. Name ="Harry"; -s =P2; -}
Reference Delivery
2. String
Important features: immutability, the manipulation of strings will create a new instance in memory
We can consider a string as a read-only group of type char.
1 Private void button2_click (object sender, EventArgs e)2 {3 string " ASDFGHJK " ; 4 MessageBox.Show (str[5]. ToString ()); 5 }
Char
3. Equals and Equals, and overrides equals
For a string type, both Equals and equals are the values themselves
Equals is the default comparison of addresses, but if we use equals in our own defined classes, we will override
1 Private voidButton3_Click (Objectsender, EventArgs e)2 {3 stringS1 ="ABC";4 stringS2 ="ABC";5 //for a string type, both Equals and equals are the values themselves6 if(S1 = =S2)7 { 8MessageBox.Show ("S1 and S2 are equal");9 }Ten Else One { AMessageBox.Show ("S1 and S2 are not equal"); - } - the if(S1. Equals (S2)) - { -MessageBox.Show ("S1 and S2 are equal"); - } + Else - { +MessageBox.Show ("S1 and S2 are not equal"); A } at - //equals is the default comparison of addresses, but if we use equals in our own defined classes, we will override -Person p1=NewPerson () {name="Liu Liu"}; - //namespace of the class -MessageBox.Show (P1. ToString ());//_1. Value passing. person - in -person P2 =NewPerson () {Name ="Liu Liu"}; to if(P1 = =p2) + { -MessageBox.Show ("P1 and P2 are equal"); the } * Else $ {Panax NotoginsengMessageBox.Show ("P1 and P2 are not equal"); - } the + if(P1. Equals (p2)) A { theMessageBox.Show ("P1 and P2 are equal"); + } - Else $ { $MessageBox.Show ("P1 and P2 are not equal"); - } - } the - class PersonWuyi { the Private stringname; - Wu Public stringName - { About Get{returnname;} $ Set{name =value;} - } - - Public Override BOOLEquals (Objectobj) A { +Person person = obj asPerson ; the if( This. name==Person . Name) - { $ return true; the } the Else the { the return false; - } in } the Public Override stringToString () the { About return "the overridden ToString ()"; the } the the}
View Code
4. Show the program run time
1 Private voidButton4_Click (Objectsender, EventArgs e)2 {3 //string str = string. Empty;4 //StringBuilder sb = new StringBuilder ();5 ////Display Run time6 //Stopwatch sp = new Stopwatch ();7 //sp. Start ();8 //for (int i=0;i<1000000;i++)9 //{Ten // //str + = i; One //sb. Append (i); A //} - //sp. Stop (); - //MessageBox.Show (sp. Elapsed.tostring ()); the //StringBuilder sb = new StringBuilder (); - //sb. Append ("gaojinding"); - //sb. Append ("Wangwu"); - //sb. Insert (3, "Lisi"); + //MessageBox.Show (sb.) ToString ()); - +}
View Code
C # review 3