1. Method overloading-[polymorphism during compilation] and heavy-load-compilation Polymorphism

Source: Internet
Author: User

1. Method overloading-[polymorphism during compilation] and heavy-load-compilation Polymorphism

Let's talk about method overloading and conceptual things. Please refer to MSDN. Let's take a look at the example below:

Example 1:

1. Create a console program: Add a class [Overload ]:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace OOP1 {/// <summary> /// polymorphism during compilation: method Overload // </summary> public class Overload {// <summary> // method Signature: contains the method name and parameters (number and number of parameters) /// </summary> /// <param name = "a"> </param> public void DisplayOverload (int a) {Console. writeLine ("DisplayOverload:" + a) ;}/// <summary> // method Signature: contains the method name and parameters (number and number of parameters) /// </summary> /// <param name = "a"> </param> public void DisplayOverload (string a) {Console. writeLine ("DisplayOverload:" + a) ;}/// <summary> // method Signature: contains the method name and parameters (number and number of parameters) /// </summary> /// <param name = "a"> </param> /// <param name = "B"> </param> public void DisplayOverload (string, int B) {Console. writeLine ("DisplayOverload:" + a + B );}}}

In the main function:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace OOP1 {class Program {static void Main (string [] args) {Overload overload = new Overload (); overload. displayOverload (1000); overload. displayOverload ("Hello C # Method Overload"); overload. displayOverload ("method overload", 2000); Console. readKey ();}}}

Run the program:

 

Summary:

1. Method overload. C # identifies the overloaded method to be called Based on the passed parameters, rather than the method name!

2. Method Signature: it consists of the method name and method parameters and the Data Type of the method!

 

Example 2:

# Region Error 1: A member with the same parameter type named "DisplayOverload" has been defined. // <summary> // the return value type of the method is not part of the method signature, therefore, these two methods are not overload methods, // </summary> // public void DisplayOverload () // {// Console. writeLine ("Hello"); //} // <summary> // The type of the return value of the method is not part of the method signature. Therefore, these two methods are not overload methods, /// </summary> // public int DisplayOverload () // {// return 1; //} # endregion

Compilation fails. Summary: The Return Value Type of a method is not a part of method overloading.

 

Example 3:

# Region Error 2: A member with the same parameter type named "DisplayOverload" has been defined // <summary> // access modifier (for example: static ), it is not part of the method signature. /// </Summary> /// <param name = "a"> </param> // static void DisplayOverload (int) /// {//} /// <summary> // The access modifier (for example, static) is not part of the method signature. /// </Summary> /// <param name = "a"> </param> // public void DisplayOverload (int) /// {//} /// <summary> // The access modifier (for example, static) is not part of the method signature. /// </Summary> /// <param name = "a"> </param> // public void DisplayOverload (string a) // {//} # endregion

Compilation fails. Summary: The method access modifier, such as static, is not part of the method signature.

 

Example 4:

In the figure above, there are three methods. during compilation, there are two errors. It is a bit strange here that I comment out, the second method, and the third method separately. After compilation: there is only one error left.

 

Conclusion: The method signature includes not only the data type of the parameter, but also the type of the parameter, such as ref and out.

So further summary: The method signature consists not only of the method name, the number of parameters, the type of parameters, but also the form of parameters. The return value of a method is not part of the method signature. The two methods cannot have the same signature, and they cannot have the same name between the members.

Zookeeper ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Next, let's talk about the Params parameter in the polymorphism [method overload] during compilation.

 

A method can be called by the following four types of parameters:

As mentioned above, the method access modifier is not part of the method signature. Now we will focus on Parameter arrays.

 

Method declaration will create an independent memory space in the memory, so:

 

In this figure, the first method, the parameter name is repeated, and the second method, because the method parameter has been declared. It is also wrong to declare a local variable with the same name in the method.

To sum up, the parameter name must be unique. In the method, we cannot already have a parameter name, declare a local variable with the same name in the Method !!!

 

 

Example 5:

/// <Summary> /// define a private field /// </summary> private string name = "daniel"; public void Display (){

Dispaly2 (ref name, ref name );
Console. WriteLine (name );

       }       private void Dispaly2(ref string x, ref string y)       {           Console.WriteLine(name);           x = "Mike 1";           Console.WriteLine(name);           y = "Mike 2";           Console.WriteLine(name);           name = "Mike 3";       }
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace OOP1{    class Program    {        static void Main(string[] args)        {                     Overload overload = new Overload();            overload.Display();            Console.ReadKey();        }    }}

Because it is passed by reference, the field value is changed in the method, and the original name field value is also changed (the Pointer Points to the same address .)

 

 Example 6:

Public void Display () {DisplayOverload (300); DisplayOverload (400, "Daniel", "Shenzhen Baoan"); DisplayOverload (800, "Wow haha ");} public void DisplayOverload (int a, params string [] parameterArray) {foreach (var item in parameterArray) {Console. writeLine (item + "" + );}}

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace OOP1{    class Program    {        static void Main(string[] args)        {                       Overload overload = new Overload();            overload.Display();            Console.ReadKey();        }    }}

Summary: using the parameter array, you can pass as many parameters as you want. The parameter array must be the last of the parameter list, for example:

 

 

Example 7:

  public void Display()        {            DisplayOverload(100, 200, 300);            DisplayOverload(200, 100);            DisplayOverload(200);        }        private void DisplayOverload(int a, params int[] parameterArray)        {            foreach (var i in parameterArray)                Console.WriteLine(i + " " + a);        }  
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace OOP1{    class Program    {        static void Main(string[] args)        {                      Overload overload = new Overload();            overload.Display();            Console.ReadKey();        }    }}

 

Conclusion: C # is very intelligent and can identify whether the second parameter is of the same type (compared with the first parameter type int)

 

Example 8;

 

Summary: The parameter array must be a one-dimensional array.

 

 

Example 9:

  public void Display()        {            string[] names = { "Akhil", "Ekta", "Arsh" };            DisplayOverload(3, names);        }        private void DisplayOverload(int a, params string[] parameterArray)        {            foreach (var s in parameterArray)                Console.WriteLine(s + " " + a);        }   

 

Summary: We allow passing an array to the parameter array, instead of a single string.

 

 

Example 10:

 

In summary, the third parameter is redundant.

 

 

Example 11:

  public void Display()        {            int[] numbers = { 10, 20, 30 };            DisplayOverload(40, numbers);            Console.WriteLine(numbers[1]);        }        private void DisplayOverload(int a, params int[] parameterArray)        {            parameterArray[1] = 1000;        }  
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace OOP1 {class Program {static void Main (string [] args) {// Overload overload = new Overload (); // overload. displayOverload (1000); // overload. displayOverload ("Hello C # Method Overload"); // overload. displayOverload ("method overload", 2000); // Console. readKey (); Overload overload = new Overload (); overload. display (); Console. readKey ();}}}

 

Summary: Because the array is a reference type, no matter what type of array, numbers [1] becomes 1000.

 

 

Example 12:

   public void Display()          {              int number = 102;              DisplayOverload(200, 1000, number, 200);              Console.WriteLine(number);          }            private void DisplayOverload(int a, params int[] parameterArray)          {              parameterArray[1] = 3000;          }  

 

Because number is a value type, the value remains unchanged before and after the method call.

 

Example 14:

public void Display()        {            DisplayOverload(200);            DisplayOverload(200, 300);            DisplayOverload(200, 300, 500, 600);        }        private void DisplayOverload(int x, int y)        {            Console.WriteLine("The two integers " + x + " " + y);        }        private void DisplayOverload(params int[] parameterArray)        {            Console.WriteLine("parameterArray");        }   

 

 

 

Conclusion: The method is called based on parameters.

 

 

So let's end with a summary:

  1. C # recognizes the method by its parameters and not only by its name. [when the method is overloaded, the C # identification method uses parameters instead of method names]
  2. The return value/parameter type of a method is never the part of method signature if the names of the methods are the same. so this is not polymorphism. [If the method name is the same, the type of the return value of the method or the type of the method parameter is never part of the method signature]
  3. Modifiers such as static are not considered as part of the method signature. [access Modifiers such as static are not part of the method signature]
  4. The signature of a method consists of its name, number and types of its formal parameters. the return type of a function is not part of the signature. two methods cannot have the same signature and also non-members cannot have the same name as members. [the signature of a method is determined by the method name, number of parameters, and form of parameters. The return value of a method is not part of the method signature. The two methods cannot have the same signature, non-members cannot have the same names.]
  5. Parameter names shocould be unique. and also we cannot have a parameter name and a declared variable name in the same function. [The parameter name must be unique. If a parameter already exists in the method, you cannot declare a local variable with the same name in the method]
  6. In case of by value, the value of the variable is ed and in the case of ref and out, the address of the reference is ed.
  7. This params keyword can only be applied to the last argument of the method. So the n number of parameters can only be at the end. [the parmas parameter must be the last of the method parameter list]
  8. C # is very smart to recognize if the penultimate argument and the params have the same data type. [C # automatically determines the data type in the parameter list]
  9. A Parameter Array must be a single dimen1_array. [The Parameter array must be A one-dimensional Array]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.