Learn 1:c#, sequential generics, generic delegates, LAMBDA, extension methods

Source: Internet
Author: User

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

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.