There are two concepts here. The method name is part of the method, such as method:
Public static void hello(int a, int b)
In this method, hello is called the method name.
Method signature refers to the construction of method name and method parameter table. Similarly, for the above method, its method signature is:
Hello (int a, int b)
Polymorphism plays a great role. As you probably know, you can compile the following code:
Use the system;
Namespace test
{
Common class MainClass
{
Public static void Main ()
{
Console. WriteLine ("hahaha");
Console. WriteLine(5);
Console. WriteLine(true);
}
}
}
The output of this code is:
hahaha
five
real
C# is a strongly typed language, and its different types must be converted before they can be processed. So why can the WriteLine () method output both string constants and integer and Boolean constants? The reason is polymorphism.
Here is an example. I wrote a class test, which contains a series of methods called Print. I handled different input parameters without overloading the WriteLine method.
//test.cs
//You can compile this code with csc test.cs or copy it into VC#.
Use the system;
Namespace TestFunc
{
Public course examination
{
Public Static Blank Print (String)
{
Console. WriteLine(str);
}
Public static invalid printing (int i)
{
Console. WriteLine(I . ToString()); //Call the ToString method to convert an integer to a string type.
}
Public static invalid printing (bool b)
{
If (b == true)// Output the judged result.
{
Console. WriteLine(" True ");
}
other
{
Console. WriteLine(" False ");
}
}
Public static void printing (paramstring [] str)
{
//This method realizes the output of an unknown number of parameters. Use params keyword.
for(int I = 0; I< Strait. Length; ++i)
{
Console. WriteLine(str[I]);
}
}
}
Common class MainClass
{
Public static void Main ()
{
bool a = false
Testing. Print ("David", "Jack", "Mike");
Testing. Printing (5);
Testing. Print (true);
Testing. Print (one copy);
Testing. Print ("Success!" );
}
}
}
The output of program execution is:
David
interrupt
microphone
five
real
wrong
It worked!
Please pay attention to the comments in the program, so that I only need one method to handle all kinds of data safely.
I hope this information is helpful to you. Thank you.