views:

305

answers:

4

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;
    }
}
+6  A: 

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; 
    } 
}
Dan
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.
Ben Voigt
+3  A: 

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;
    }
}
yodaj007
You can do that. There's a big question of whether you should. :)
pdr
@pdr: ?? The question is why the method has to be static. My answer is that it doesn't have to be, and demonstrates it.
yodaj007
+1  A: 

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.

tvanfosson
A: 

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.

sirchristian
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.
Rune FS