C # Generics and LINQ

Source: Internet
Author: User

Generic type

We look at the code in the class to see which types need to be modified, and each time we need a new type (long,double,string), we need to repeat the process. After these processes, we have a lot of classes with almost the same code, which takes up extra space. Debugging and maintaining this code is complex and error-prone.

Definition: Generics (generic) can fudge multiple types share a set of code, generics allow us to declare type parameterization. It is possible to instantiate with different types, and plainly, it is possible to use type placeholders to create real-life concepts of specific types that are deadly. There are five generics, classes, structs, interfaces, delegates, and methods available in C #.

Class mystack<t>    {        t[] statkarray;        int stackpointer = 0;        const int maxstack = ten;        BOOL Isstackfull {get {return stackpointer < maxstack;}}        BOOL Isstackempty {get {return stackpointer <= 0;}}        public void Push (T x)        {            if (! Isstackfull)            {                statkarray[stackpointer++] = x;            }                              }        Public T Pop ()        {            return (! isstackempty)? Statkarray[--stackpointer]:statkarray[0];        }        Int[] arr={1,2,3,4};        public void Print ()        {for            (int i = stackPointer-1; i>=0; i--)            {                Console.WriteLine ("Value;{ 0} ", Statkarray[i]);}}        

static void Main (string[] args)        {            mystack<int> stackint = new mystack<int> ();            mystack<string> stackstring = new mystack<string> ();            Stackint. Push (3);            Stackint. Push (5);            Stackint. Push (7);            Stackint. Push (9);            Stackint. Print ();            Stackstring. Push ("Hava a Good Day");            Stackstring. Push ("Hi there!");            Stackstring. Print ();            Console.readkey ();        }

Generic delegate:

Class simple    {        static public void printstring (string s)        {            Console.WriteLine (s);        }        static public void printupperstring (string s)        {            Console.WriteLine ("{0}", S.toupper ());        }    } Delegate  void mydelegate<t> (T value);    Class program    {        static void Main (string[] args)        {            var mydel = new mydelegate<string> (simple). printstring);            Mydel + = simple. printupperstring;            Mydel ("Rocky");            Console.readkey ();        }    }

Generic interface:

Interface imyfc<t>    {        t Returnit (t invalue);    } Class simple<s>:imyfc<s>    {public        s Returnit (S invalue)        {            return invalue;        }    } Class program    {        static void Main (string[] args)        {            var trivint = new simple<int> ();            var trivstring = new simple<string> ();            Console.WriteLine (Trivint.returnit (5));            Console.WriteLine (Trivstring.returnit ("Zhang San"));            Console.readkey ();        }    }

Linq:

Class program    {        static void Main (string[] args)        {            int[] array = {One,            one, one, and one}; Func<int, int> func = new Func<int, int> (Method);            var arr = array. Select (func);            foreach (var item in arr)            {                Console.WriteLine (item);            }            Console.readkey ();        }        static int Method (int x)        {            if (x = =)            {                return x;            }            else            {                return 0;}}    }

C # Generics and LINQ

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.