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.