plc.myforum.ro Forum Index plc.myforum.ro
PLCs users www.plc.myforum.ro!
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Understanding Delegates in C# for Beginners

 
Post new topic   Reply to topic    plc.myforum.ro Forum Index -> Programming
View previous topic :: View next topic  
Author Message
kosta
Member
Member


Joined: 09 Aug 2015
Posts: 161
Location: Salonic

PostPosted: 13/12/2017 20:25    Post subject: Understanding Delegates in C# for Beginners Reply with quote

Understanding Delegates in C# for Beginners
Delegates as a technical concept in C# create a lot of confusion in the beginners’ mind. It is a fairly simple concept, but most of the examples floating around on the web are so trivial that they do not expose the real benefits of delegates. Look at the below sample:

Code:
public delegate dublu Delegate(int a,int b);
class Class1
{
    static double fn_ToHookToTheDelegate(int val1, int val2)
    {
        return val1 * val2;
    }

    static void Main(string[] args)
    {
        //Creating the Delegate Instance
        Delegate delObj = new Delegate(fn_ToHookToTheDelegate);
        Console.Write("Please Enter Values");
        int v1 = Int32.Parse(Console.ReadLine());
        int v2 = Int32.Parse(Console.ReadLine());
        //Call delegate for processing
        double res = delObj(v1, v2);
        Console.WriteLine ("Result: " + res);
        Console.ReadLine();
     }
}


In this example, everything exists within the confines of the same class. The delegate (or the hook, as I call it) and the function to be hooked to it are both in the same class. The following line of code in the Main() makes no pragmatic sense:
Code:

Delegate delObj = new Delegate(fn_ToHookToTheDelegate);


Please read the following articles before proceeding with this article:
http://msdn.microsoft.com/en-us/library/ms173171%28v=vs.90%29.aspx
http://msdn.microsoft.com/en-us/library/ms173172%28v=vs.90%29.aspx
Reading the above-mentioned articles is NOT optional.
read on
https://www.codeproject.com/Articles/884981/Understanding-Delegates-in-Csharp-for-Beginners
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    plc.myforum.ro Forum Index -> Programming All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Fa-ti si tu propriul forum GRATUIT, pe www.myforum.ro