A little thought about generic classes and extension methods

Source: Internet
Author: User

We often create generic instances through generic constructors, and we often call extension methods for instances. The following code is ubiquitous in the project:

        static void Main (string[] args)
        {
            var STRs = new List<string> {"Hello", "World"};
            var result = STRs. Where (s = = S.startswith ("h"));
            foreach (var item in result)
            {
                Console.WriteLine (item);
            }
            Console.readkey ();
        }

Above

How is the internal implementation of an instance created by a generic collection list<t> constructor?

Using the Where method for the instance variable STRs, we find that the where is also sufficiently "smart" because it is an extension method for the IEnumerable collection type.

. How is the net interior actually implemented? Try to copy it ~ ~

Creates a type generic.

    public class Mycute<T>
    {
        Public Mycute (T t)
        {
            Getcute = t;
        }
        Public T Getcute {get; set;}
    }    

This is called on the client:

        static void Main (string[] args)
        {
            var cuteint = new Mycute<int>(10);
            var cutestr = new Mycute<string>("Hello");
            Console.WriteLine (Cuteint.getcute);
            Console.WriteLine (Cutestr.getcute);
            Console.readkey ();
        }

Above, perhaps the revelation is: if you want to create a generic instance through a constructor, you need to define a generic class, a generic type attribute, and a constructor that takes a generic type as a parameter.

Now you want to use the extension method for the properties of the Cuteint and cutestr of the generic instance, how do you do that getcute?

The Cuteint property Getcute is of type int, Cutestr's property Getcute is a string type, and the common base class for both is object, which is to write an extension method for the object type.

    public static Class Myhelper
    {
        public static string Getstr (This object obj)
        {
            return obj. ToString () + "--added string";
        }
    }

The client becomes like this:

        static void Main (string[] args)
        {
            var cuteint = new Mycute<int>(10);
            var cutestr = new Mycute<string>("Hello");
            Console.WriteLine (CuteInt.GetCute.GetStr ());
            Console.WriteLine (CuteStr.GetCute.GetStr ());
            Console.readkey ();
        }    

Summarize:

If the operation logic is the same for different types, you can abstract a generic class. There is no essential difference between a generic class and a normal class, except that a placeholder, or type parameter is appended to the class name, and the constructor parameter of the generic class is the type parameter, and the property type of the generic class is also the type parameter.

If the operation logic is the same for different types of instances, you can write an extension method for the common parent class or interface of the different instance types.

A little thought about generic classes and 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.