For this you need delegates?

Hey all, just started to learn delegates. Included here is an example:
// Class method will be notified with the delegate.
 class MyClass
 // Create the method that you plan to communicate with the delegate.
 public string Method(string name)
 return "Hello" + name;

 // 21-th line create a class-delegate named MyDelegate,
 // method that will be notified with an instance of this class delegate 
 // will accept a single string argument and return a string value.

 public delegate string MyDelegate(string name); // Create a delegate class. (1) 

 class Program
 static void Main()
 MyClass instance = new MyClass();

 MyDelegate myDelegate = new MyDelegate(instance.Method); // Create an instance of the delegate and tell him the method. (2)

 string greeting = myDelegate.Invoke("djon"); // Call this method with a delegate. (3)


 greeting = myDelegate("Grady Booch"); // Another way to call this method with a delegate. (3')


 // Delay.

for fun I replaced the line:
string greeting = myDelegate("djon"); // Call this method with a delegate. (3)

greetingg string = MyClass.Method("djon");
and got the following error from Visual Studio 2012 :
Error 1
For the non-static field, method, or property "Delegates.MyClass.Method(string)" requires a reference to the object

After all, a delegate is an object containing pointers to methods.

Ie this example of an error committed by me for the sake of experiment, is evident or you can say life example of why you need to use delegates?

If so, then good, if not please give your life examples of using delegates.
July 12th 19 at 16:48
2 answers
July 12th 19 at 16:50
Delegates is an implementation of the functor on the platform .net. The functor is common in modern languages the basic concept that has come from functional programming. Briefly its purpose is to work with the function as a value, along with values of other, more familiar types, like int or double. I.e. you can pass a function into other functions, return from functions, and so forth. In functional languages is common, however, in imperative languages it gives a lot of opportunities.

The number of applications delegates will dotnet in General and specifically in C# is huge. In fact, the language today is partly functional due to the fact that widely used by delegates and based on their ability. Any lambda can be compiled into a delegate. LINQ to Objects is based on delegates. Events are encapsulated delegates. Any callback and it will dotnet delegates. The list goes on

Comparing C# with other languages, delegates are often called "smart pointers to functions" in the sense that they are similar to function pointers in C, but also are able to remember the object for which you want to invoke the method if the method is nonstatic. But in C++ there is no direct analog thereof is std::function.

In General, read more about them. I don't even know why you brought your example, and that he, in your opinion, should demonstrate. It is logical that you are unable to create the delegate, because the instance method cannot be invoked without reference to a specific object.
July 12th 19 at 16:52
No, it's not a realistic example. This is a very strange example, and no one in their right mind would call a non-static method on a type name. In this case, racially correct and the only rational solution would be to refer to this method as the var ret = instance.Method("string");.

However, looking back, actually, to the theme of the delegates themselves and their life applications. The most classic example would be the implementation of the function Where (which comes in LINQ).
// in static class
IEnumerable<telement> Where(this IEnumerable<telement> source, Predicate<telement> filter){
 // for simplicity we omit the check, etc.
 foreach(TElement elem in source)
 if(filter(elem)) yield return elem;
// ... usage ...
var list = new list<int> { 1, 2, 3, 4, 5, 6 };
var ret = list.Where(elem => elem % 2 == 0);
foreach(int e in ret)
 Console.Write(e + " ");
// ...</int></telement></telement></telement>

In this case, the function loops through each element in the enumeration source, calls the function filter (a function that takes a value of type TElement and returns a bool) and returns the current item if the returned filter value was true.
After executing the snippet, the screen would be output "2 4 6 ".

Find more questions by tags C#