Several code reuse methods in C #

Source: Internet
Author: User

If there are multiple methods in the project, their start and end are similar, but they do not change only the middle part of the logic. For example:

static void f1()        {            Console.WriteLine("begin");            Console.WriteLine("do something1!");            Console.WriteLine("end");        }        static void f2()        {            Console.WriteLine("begin");            Console.WriteLine("do something2!");            Console.WriteLine("end");        }

For general calls, the statement is as follows:

f1();f2();

So how can we reuse the public part? You can use delegation. Design a public class that contains a public static method:


class Common    {        public static void f(Action action)        {            Console.WriteLine("begin");            action();            Console.WriteLine("end");        }    }

Therefore, you only need to implement the following functions during the call:

Common.f(() => Console.WriteLine("do something 1111!"));Common.f(() => Console.WriteLine("do something 2222!"));

In the above process, there is a premise for the extraction of f1 () and f2 () methods, that is, the input parameters of f1 () and f2 () are the same, and there is no input parameter here ). This method can be used only when the input parameters are the same.

How to extract the public part of any input parameter method?

One method is to overload multiple generic parameters for public methods:


class Common    {      public static void f(Action action)        {            Console.WriteLine("begin");            action();            Console.WriteLine("end");        }        public static void f<T1>(Action<T1> action,T1 arg1)        {            Console.WriteLine("begin");            action(arg1);            Console.WriteLine("end");        }        public static void f<T1, T2>(Action<T1, T2> action, T1 arg1, T2 arg2)        {            Console.WriteLine("begin");            action(arg1, arg2);            Console.WriteLine("end");        }  }

You can overload multiple methods as needed. In this way, it is convenient to call.

Assume the following method:


static void f1(int i)        {            Console.WriteLine("begin");            Console.WriteLine(i);            Console.WriteLine("end");        }        static void f2(int i, string s)        {            Console.WriteLine("begin");            Console.WriteLine(i); Console.WriteLine(s);            Console.WriteLine("end");        }

The input parameters are different. After extraction through the above method, you only need to write the following code.

Common.f<int>((i) => Console.WriteLine(i), 3);Common.f<int, string>((i, s) => { Console.WriteLine(i); Console.WriteLine(s); }, 3, "www");


Another method is to use the Delegate class.

The implementation is as follows:

public static void f(Delegate exp, params dynamic[] d)      {          Console.WriteLine("begin");          exp.DynamicInvoke(d);          Console.WriteLine("end");      }

In the code, use the Delegate class to call the Delegate through the DynamicInvoke method. This method is simpler. You only need the following code for calling:

Action<int> p = (a) => Console.WriteLine(a);Action<int, string> p2 = (a, s) => { Console.WriteLine(a); Console.WriteLine(s); };Common.f(p, 3);Common.f(p2, 3,"ww");

Note: The Delegate class is not the Delegate type. The delegate class is used to obtain the delegate type. It can be considered that the delegate type is an instance of the Delegate class.

This article is from the "one blog" blog, please be sure to keep this source http://cnn237111.blog.51cto.com/2359144/1205719

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.