C #3.0 Extensions method and common auxiliary method set

Source: Internet
Author: User

Many functions of C #3.0 are discussed in the discussion. It is probably that the syntax of LINQ is really novel for C # and other languages. However, Extension Method is rarely discussed, even though it is one of the important cornerstones of LINQ. Maybe you think this is too simple to mention? However, in my projects, there are actually many places that apply to the Extension Method, but you are not aware of it.

One major purpose of Extension Method is to construct an auxiliary Method. To abstract and simplify the programming process, we will put some common methods that are difficult to create an object class for them into the so-called Helper, and call Helper. Xxxx () during use (). For example:

public static class Helper{    public static SecureString ToSecureString(string value)    {        SecureString result = new SecureString();        foreach (char c in value.ToCharArray())        {            result.AppendChar(c);        }        return result;    }    public static T ToEnum<T>(string value, bool ignoreCase) where T : struct    {        return (T) Enum.Parse(typeof(T), value, ignoreCase);    }}

We call Helper. toSecureString ("something"), Helper. toEnum <BrowserType> (input, true) and so on, but various methods for different purposes are referenced by the Helper class, which makes people feel a bit nondescribable. If the Extension Method is used, the process may be much simpler:

public static class Extensions{    public static SecureString ToSecureString(this string value)    {        // convert to SecureString    }    public static T ToEnum<T>(this string value, bool ignoreCase) where T : struct    {        // convert to enum.    }}

It will also be much cleaner:

static void Method(string password, string browserString){    SecureString secured = password.ToSecureString();    BrowserType browerType = browserString.ToEnum<BrowserType>(true);}

The essence of Extension Method is actually a compiler magic, and the compiler will put password. replace ToSecureString () with Extensions. toSecureString (password), the compiled and produced code can be completely in.. NET 2.0. In addition, due to the use of the Extension Method, the Customer Code does not reference the class name "Extensions" or "Helper", so when there are more and more Helper methods, we need to split the Helper/Extension class into StringHelper, when XmlHelper and EnumHelper, the Customer Code does not need to be modified. In fact, this is also in line with a design principle-Open Close principle.

Some people disagree with Extension and think that the use of Extension indicates that the design of the original type or interface is incomplete or not careful. One of the principles of type design is a single responsibility. Irrelevant methods should not be used to pollute the type itself. For example, should ToSecureStirng be a string method? Should ToEnum be its function? Of course not. Extension Method is used to provide convenience, improve readability, or provide some bonding capabilities.

One of my jobs is to write some Common types and methods for the team. I will list some of my commonly used Extension methods in subsequent articles. This is a directory.

My frequently used auxiliary methods include:

Common auxiliary method set -- Enumerable

 

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.