Recently again back to learn C #, only to find that the knowledge before learning how simple, some things are very difficult to understand, but more interesting, such as covariance, inversion, not change is very interesting, but also quite around, the following is the learning process to write some code, a little messy, when the diary narrative.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacegenericity Generic {//Generic Delegate Public DelegateR Mydelegate<t, r>(T T); classProgram {Static voidMain (string[] args) {Mystack<int> stack =Newmystack<int> (5); varStack1 =Newmystack<string> (8); while(!stack. Isfull) {stack. Push (2); } while(!stack. IsEmpty) {stack. Pop (); } mydelegate<string,string>s; S=stack. printstring; //lambda expressions + = (stringa) = = {returnA;}; Console.WriteLine (S ("Safa")); Console.WriteLine ("The length of this stack is: {0}", Stack. getlength<int>()); Console.readkey (); } } //sequential stacks, generics Public classMystack<t>{t[] stackarray; Public intLength; intStackpointer; Public stringPrintstring (stringa) {returnA; } Public BOOLIsEmpty {Get { returnStackpointer = =0?true:false; } } Public BOOLIsfull {Get { returnStackpointer >= Length?true:false; } } PublicMystack (int_length) {Length=_length; Stackarray=NewT[length]; Stackpointer=0; } Public voidPush (T _value) {if(Isfull) {Console.WriteLine ("Stack is full"); } stackarray[stackpointer++] =_value; } Public voidPop () {if(IsEmpty) {Console.WriteLine ("stack is empty"); } Console.WriteLine (stackarray[--Stackpointer]); } Public voidStackprint () {foreach(varIinchStackarray) {Console.WriteLine (i); } } } //Expansion Methods Public Static classExtendstack { Public Static intGetlength<t> ( ThisMystack<t>ms) { returnMs. Length; } }}
Learn 1:c#, sequential generics, generic delegates, LAMBDA, extension methods