Provide alternative implementation for tool functions in the project

Source: Internet
Author: User

Some tool classes are often written during project development. In these classes, no specific business is actually done, but some implementations are provided for some logic and algorithms. For example, some operations on formatting strings and obtaining connected strings. To make these methods generic, they can be conveniently called. Some static classes are often designed and implemented using static methods. This implementation is very good, but it is quite troublesome. For example, you have designed a static class called commonfunctions, which contains many static classes. One of the methods is called dosamething, which is often called. Therefore, every time you call it, you need to write it like this:

CommonFunctions.DoSameThing()

Every time you add a prefix such as commonfunctions, it is not very neat and difficult to write. As a result, I first thought of the static import of Java. in Java, such implementations are usually written at the top of the file:

import static packagename.CommonFunctions.DoSameThing;

In this way, redundant prefixes such as commonfunctions can be removed. However, the tragedy is that C # does not have a similar static import mechanism. To achieve the same thing, after some time of thinking and collecting some information, I finally found that. net Framework 3.0 has an additional mechanism called class extension.

Therefore, in order to achieve the capability of similar static import, you need to write as follows:

namespace myNamespace
{
    static class CommonFunctions
    {
         public static void DoSameThing(this Object obj)
         {
             //do samething you want
         }
    }
}

Then you can use the namespace where you need to use this method. Here, the first parameter in the dosamething parameter list is this classname o, which means this method is used to extend the classname class, that is, all instances of the classname class, the dosamething method will be available in the future. In the above example, the object is directly extended, because all classes in C # inherit from the object directly or indirectly, after using this namespace, all classes have this method. When calling, you only need to write:

DoSameThing();

You can. If the extension method you designed has multiple parameters, you can directly write your parameter to this classname obj. For example, I have a method prototype like this:

public String OpsStr(String s1, String 2);

To extend this function to the string class, so that all future string objects can be used, you can achieve this:

namespace myNamepace
{
      static class StringFunc
      {
           public String OpsStr(this String s, String s1, String s2)
           {
             //do samethin you want
           }
      }
}

This is an extension of the string class. For the purpose of designing tool functions, directly extending the object class is a good choice.

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.