I have long wanted to write some of my understandings when I was learning C #, but I haven't written them all the time. The main reason is that I am afraid the experts will say that my articles are too poor and I have not posted them on my blog.
Qualifications of the park. However, I also want to give some suggestions to help me better study. I also hope to provide some help to C # beginners. Finally
I decided to write this series with a strong scalp, and I could not name it a series. In fact, it is some important concepts that I think may not exist.
Any link. But all the important concepts are simple, so I will start with these simple usage.
Article 1 delegate and anonymous methods:
A delegate is a type of reference method. Once a method is assigned to the Delegate, the delegate has the same behavior as the delegate. Delegate Method
A call can have parameters and return values like any other method, as shown in the following example:
Public Delegate int implements mcalculation (int x, int y );
Any method in the class or structure that matches the delegate signature (composed of return types and parameters) can be assigned to the delegate.
The method can be static or instance. In this way, you can change the method call by programming, and
Insert new code. As long as you know the signature of the Delegate, you can allocate your own delegate method.
The following is a simple example;
Delegate double mydelegate (double A, double B); // declare the delegate
Static double multiply (double A, double B) // defines the multiplication method.
{
Return a * B;
}
Static double divide (double A, double B) // defines the division method.
{
Return A/B;
}
Static void operation (string o, double A, double B)
{
Mydelegate MYDEL; // instantiate a delegate
If (O = "M ")
MYDEL = new mydelegate (multiply );
Else
MYDEL = new mydelegate (divide );
Console. writeline ("jieguo Shi: {0}", MYDEL (a, B ));
}
In this example, different methods can be called based on different input operators to perform different operations on the same parameters. Let's look at this
The main purpose of the example is to illustrate some anonymous methods. First, let's take a look at what is an anonymous method. An anonymous function is an "inline" statement or expression.
Type, which can be used wherever the delegate type is required. You can use anonymous functions to initialize the name delegate, or pass the name delegate (instead
Name the delegate type) as the method parameter. I understand the anonymous method as a simplification of delegation. Next, let's take a look at this Code:
Static void anyoperation (string o, double A, double B)
{
Mydelegate mydele;
If (O = "M ")
Mydele = delegate (double C, double D) {return C * D ;};
Else
Mydele = delegate (double C, double D) {return C/d ;};
Console. writeline ("jieguo Shi: {0}", mydele (a, B ));
}
You can clearly understand what Anonymous means by comparing the difference between this code and the previous code.
If you have a name, you can directly use the method body. This is because your understanding is not necessarily correct. Please do not hold your account accountable. Note that
In the parameter section of the algorithm, you must find out which parameters are delegated.
Next we will put the code of the entire program. If you are interested, you can run it on your own machine.
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace weituo
{
Class Program
{
Static void main (string [] ARGs)
{
Double A = 0, B = 0;
String c = NULL;
Console. writeline ("Qing shuru Liangge shuzi, ranhou shuru caozuofu ('M' or 'D ')");
A = double. parse (console. Readline ());
B = double. parse (console. Readline ());
C = console. Readline ();
Console. writeline ("shiyong weituo :");
Operation (C, A, B );
Console. writeline ("shiyong nimingfangfa :");
Anyoperation (C, A, B );
Console. readkey ();
}
Delegate double mydelegate (double A, double B); // declare the delegate
Static double multiply (double A, double B) // defines the multiplication method.
{
Return a * B;
}
Static double divide (double A, double B) // defines the division method.
{
Return A/B;
}
Static void operation (string o, double A, double B)
{
Mydelegate MYDEL; // instantiate a delegate
If (O = "M ")
MYDEL = new mydelegate (multiply );
Else
MYDEL = new mydelegate (divide );
Console. writeline ("jieguo Shi: {0}", MYDEL (a, B ));
}
Static void anyoperation (string o, double A, double B)
{
Mydelegate mydele;
If (O = "M ")
Mydele = delegate (double C, double D) {return C * D ;};
Else
Mydele = delegate (double C, double D) {return C/d ;};
Console. writeline ("jieguo Shi: {0}", mydele (a, B ));
}
}
}