Get value from a Delegate in C#

Shervan360 1,661 Reputation points
2023-06-20T01:24:52.81+00:00

Hello,

How can I get value (here value is 100) from a delegate?


public delegate int MyDel(int x);
internal class Program
{
     static MyDel MyMethod(int x)
     {
          MyDel my = x => x * x;
          return my; //100
     }
     static void Main()
     {
          Console.WriteLine(MyMethod(10));
     }
}


Developer technologies .NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-06-20T02:37:37.7+00:00

    Hi @Shervan360 , Welcome to Microsoft Q&A.

    We generally don't use it like this: you can use the following two ways

    using System;
    
    namespace_6_20_x
    {
         public delegate int MyDel(int x);
         internal class Program
         {
             static int Square(int x)
             {
                 return x * x;
             }
             static void Main(string[] args)
             {
                 MyDel myDelegate = new MyDel(Square); // Create a delegate instance and bind it to the Square method
                 int result = myDelegate(10); // call the delegate instance, passing the parameter 10
                 Console.WriteLine(result); // Output: 100
             }
         }
    }
    

    using System;
    
    namespace _6_20_x
    {
        public delegate int MyDel(int x);
        internal class Program
        {
            static void Main(string[] args)
            {
                MyDel myDelegate = x => x * x; // use lambda expression to create delegate instance
                int result = myDelegate(10); // call the delegate instance, passing the parameter 10
                Console.WriteLine(result); // Output: 100            
            }
        }
    }
    

    In your code, MyMethod(10) returns type MyDel. You have to nest one layer to get the value out.

    using System;
    
    public delegate int MyDel(int x);
    
    internal class Program
    {
        static MyDel MyMethod(int x)
        {
            MyDel my = y => y * y;
            return my;
        }
    
        static void Main(string[] args)
        {
            MyDel myDelegate = MyMethod(10);
            int result = myDelegate(0); // Invoking the delegate with any value (0 in this case)
            Console.WriteLine(result); // Output: 100
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.