Use C #3.0 extensions

Source: Internet
Author: User

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.

Related Article

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.