01-C # entry (function overloading and delegation)

Source: Internet
Author: User

  • Function Overloading

It is easier to understand than delegate.

Involves a concept:Function Signature.The function signature includes the function name and parameters.Function overload: The function is implemented using the same name and different parameters. The same function name and parameter cannot be declared, but different return types are not considered as function overloading. Because the function signature does not include the return type, therefore, this is equivalent to repeating the definition of a function, and an error is certainly returned.

        static int MaxVal(int[] intArrayVal)        {            int maxVal = intArrayVal[0];            for (int i = 0; i < intArrayVal.Length; i++)            {                if (intArrayVal[i] > maxVal)                {                    maxVal = intArrayVal[i];                }            }            return maxVal;        }        static double MaxVal(double[] doubleArrayVal)        {            double maxVal = doubleArrayVal[0];            for (int i = 0; i < doubleArrayVal.Length; i++)            {                if (doubleArrayVal[i] > maxVal)                {                    maxVal = doubleArrayVal[i];                }            }            return maxVal;        }

Declare the version () of the two functions. When using the function, enter the function name MaxVal (to get the prompt information. You can find that there are two types of parameters. You only need to enter parameters of the matching type:

Int [] myInt = {1, 3, 4, 5}; double [] myDouble = {3.14, 3.15, 3.22, 3.01}; Console. writeLine ("Maximum myInt value is {0}", MaxVal (myInt); Console. writeLine ("myDouble max value is: {0}", MaxVal (myDouble ));

Of course, the overload also includes different parameter types (), which can implement the same function and different value transfer processing methods ().

  • Delegate

After the delegate is defined, you can declare the variable of the delegate type. Then, the variable is initially referenced by a function with the same return type and parameter list as the delegate. Then, you can use the delegate variable to call this function, just as the variable is a function. Let's look at an example:

Class Program {// define the delegate: variables of the delegate type can be declared later; // initialize the delegate variable: The variable can be initialized as a reference of the function, during initialization, you must specify the corresponding function name. // these functions must have the same return type and parameter list as the delegate. // use the delegate variable: when using this delegate variable, the usage is the same as the function (including the return value); // think? If you do not need to delegate, Judge user input, and then call the corresponding function, the same effect can be achieved ...... // Error. The key point is that the delegate variable can be passed to a function as a parameter, so that the function can use the delegate to call any function referenced by it, you do not need to know which function to call before running it. Delegate double ProcessDelegate (double param1, double param2); static double Multiply (double param1, double param2) {return param1 * param2;} static double Divide (double param1, double param2) {return param1/param2;} static void Main (string [] args) {ProcessDelegate process; Console. writeLine ("enter two numbers separated by commas (,); string input = Console. readLine (); int commaPos = input. indexOf (','); // declare to initialize two double values, Convert from the number obtained above, and use commaPos to determine the value content double param1 = Convert. toDouble (input. substring (0, commaPos); double param2 = Convert. toDouble (input. substring (commaPos + 1, input. length-commaPos-1); Console. writeLine ("input M to multiply or input D to perform the division operation:"); input = Console. readLine (); if (input = "m") // initialize the delegate variable, specify the delegate type (ProcessDelegate created above), and provide a parameter to reference the function, // This is a syntax for delegate value assignment. in brackets, specify the corresponding function name (without parentheses). // there is also a simple usage, process = Multiply; // The Compiler automatically matches the return type and parameters of the delegate variables and functions. If they do not match, an error is returned. process = new ProcessDelegate (Multiply); else process = new ProcessDelegate (Divide ); console. writeLine ("depending on your choice, different functions are called during delegation instantiation;"); Console. writeLine ("and, in this step, pass the corresponding parameter to the Delegate (Delegate to the function), calculation result: {0}", process (param1, param2); Console. readKey ();}}

Key points:You can pass the delegate variable as a parameter to a function. In this way, the function can call any function referenced by the delegate, and you do not need to know which function is called before running.

  • Conclusion

I have seen my colleagues demonstrate the delegation mechanism used in the project. Although I have clarified the principle here, the in-depth use is still vague, however, it is said that it is easier to understand the content related to event processing in chapter 13.

I finally finished reading this important article. However, I still cannot say that I have mastered it without systematic case studies.

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.