Why can't a delegate refer to a non-static method when used in a static method?
Asked Answered
I

5

7

Why is it necessary to make a function STATIC while using delegates in C# ?

class Program
{
    delegate int Fun (int a, int b);
    static void Main(string[] args)
    {
        Fun F1 = new Fun(Add);
        int Res= F1(2,3);
        Console.WriteLine(Res);
    }

   **static public int Add(int a, int b)** 
    {
        int result;
        result = a + b;
        return result;
    }
}
Inessa answered 19/2, 2010 at 19:26 Comment(2)
Because you are trying to reference it in a static context from Main?Viscoid
I deny the premise of the question; it is not necessary to make the method static. It is necessary to provide enough information to the delegate to successfully call the method; for non-static methods, that includes providing the instance that is receiving the non-static method call.Giraffe
B
14

It's not "necessary". But your Main method is static, so it can't call a non-static method. Try something like this (this isn't really a good way to do things—you really should create a new class, but it doesn't change your sample much):

class Program 
{ 
    delegate int Fun (int a, int b); 
    void Execute()
    {
       Fun F1 = new Fun(Add); 
       int Res= F1(2,3); 
       Console.WriteLine(Res); 
    }

    static void Main(string[] args) 
    { 
        var program = new Program();
        program.Execute();
    } 

    int Add(int a, int b)
    { 
        int result; 
        result = a + b; 
        return result; 
    } 
}
Beowulf answered 19/2, 2010 at 19:28 Comment(1)
Good job disproving your own claim with code. Clearly Main (a static method) refers to Execute (an instance method) by calling it. The correct statement would be "For a static method or method of another class to refer to an instance method, it must provide the instance when doing so". Instance methods within the same class also must provide an instance when referring to an instance method (check the IL to see this), but the C# compiler defaults to "this" is you don't specify one.Klotz
U
9

Your function needs to be static because you're calling from a static method, Main. You can make the method non-static:

class Program
{
    delegate int Fun (int a, int b);
    static void Main(string[] args)
    {
        Program p = new Program();       // create instance of Program
        Fun F1 = new Fun(p.Add);         // now your non-static method can be referenced
        int Res= F1(2,3);
        Console.WriteLine(Res);
    }

    public int Add(int a, int b)
    {
        int result;
        result = a + b;
        return result;
    }
}
Uzzi answered 19/2, 2010 at 19:29 Comment(0)
S
1

In this case, because you aren't creating an instance of any class, the only alternative is a static function. Were you to instantiate an object of type Program, then you could use an instance method instead.

Simpleminded answered 19/2, 2010 at 19:29 Comment(0)
D
1

Delegates basically follow the same rules as methods. In the example provided your delegate must be static because you are calling it from a static method. In the same vein this will not work:

static void Main(string[] args)
{
    int Res = Add(3, 4);
    Console.WriteLine(Res);
}

public int Add(int a, int b)
{
    int result;
    result = a + b;
    return result;
}

However if you moved things into a non static context like this:

class MyClass
{
    public MyClass()
    {
        Fun F1 = new Fun(Add);
        int Res = F1(2, 3);
        Console.WriteLine(Res);
    }

    public int Add(int a, int b)
    {
        int result;
        result = a + b;
        return result;
    }
}

You can have a delegate with a non-static method.

Demagogic answered 19/2, 2010 at 19:35 Comment(1)
That's not true you can delegate to a non-static method from a static context. E.g. MyDelegate del = foo.Bar; is valid even if Bar is not static and the assignment is in a static context.Ticino
U
-1

No need to create a static method to pass in delegate.

But the non static method should be declared in different class and have to be accessed with instance of that class.

DelegateName DN = new DelegateName ( instance of the class . Method Name)

Ussery answered 12/6, 2018 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.