1. Single-case mode
///<summary> ///Singleton mode ///</summary> ///<typeparam name= "T" ></typeparam> Public classsingleton<t> where T:New () { Public StaticT Instance {get {returnsingletoncreator.instance;} } Public classSingletoncreator {internal ReadOnlyStaticT instance=NewT (); } }
2. Bubble sort
///<summary> ///Bubble Sort ///</summary> Public Static voidMaopao () {int[] Arry = {30, 6, 5, 15, 13, 18, 10, 36, 25 }; for(inti = 0; I < Arry. Length-1; i++) { for(intj = i + 1; J < Arry. Length; J + +) { inttemp; if(Arry[i] >Arry[j]) {Temp=Arry[i]; Arry[i]=Arry[j]; ARRY[J]=temp; }}} foreach (var item in Arry) {Console.Write ("-" + Item + "-"); } } }
3. The factorial of the recursive calculation 5, I only calculated the final result, can go into the point of the words, interested can write a 5! =5*4*3*2*1,4! =4*3*2*1 this output, a slight change should be simple
// /<summary> // // /</summary> static int recursion (int m) { if (M <= 1) return 1; else { return m * Recursion (M-1); } }
4. Do not use the third variable to exchange two variable values, (write a cool point, then use bit arithmetic it, the first kind I commented out)
Public Static voidJiaohuan () {//int i = 500; //int j = Int. MaxValue-10; ////int i = ten; ////int j = 20; //Console.WriteLine ("I={0},j={1}", I, j); //i = i + j;//i=30//j = i-j;//j=10; //i = i-j;//i=20; intm = 9; intn = 5; Console.WriteLine ("Initial value"); Console.WriteLine (m); Console.WriteLine (n); Console.WriteLine ("Intermediate Change"); M= m ^N; Console.WriteLine (m); N= n ^m; Console.WriteLine (n); M= m ^N; Console.WriteLine ("Swapped value"); Console.WriteLine (m); Console.WriteLine (n); }
5. This kind of inheritance of the basis of the question is very common, to analyze, to be studied, what shortcomings, you can exchange the relationship between the class
#region base class inheritance, overriding the problem Public classFruit { PublicFruit () {fruitname= "Fruit"; Writefruitname (); } PublicVirtualvoidWritefruitname () {Console.WriteLine ("Fruit ' s Writefruitname (), the Name is" +fruitname); } protectedstring Fruitname {get; set;} } Public classBanana:fruit { PublicBanana () {fruitname= "Banana"; //writefruitname ();//in this call and do not invoke two distinct results } PublicOverridevoidWritefruitname () {Console.WriteLine ("Banana ' s Writefruitname (), the Name is" +fruitname); }} #endregion
Call the output results, pay attention to the comments oh, write a very classic, very sorry, that is I picked someone else's hand, do not understand, you can debug, see the running process, how to call, perhaps you will suddenly,
// The execution result is: Banana's Writefruitname (), the Name is Fruit. // The reason is that when the virtual method is called, it is found that the subclass has loaded the method, so call the writefruitname of the Banana class, // But at this point the constructor of the banana class has not yet been executed, that is, the Fruitname property has not been assigned to the banana class, and the constructor of the base class assigns it to fruit. New Banana (); // The execution result is: Banana's Writefruitname (), the Name is Banana. b.writefruitname ();
Because of the time, to work, Factory mode, have not written, and then write it, there is nothing wrong place can leave a message, we exchange together
Nothing to do, the recent encounter of some of the facet test, single-case mode, bubble sort, recursive