Rodniko
Rodniko

Reputation: 5124

why does a function that added to delegate needs to be static?

I declared a delegate:

public delegate void Del(string message);

Then i created a function that i want to add to the delegate:

public static void DelegateMethod(string message)
{
    System.Console.WriteLine(message);
}

Now I add the function to the delegate and call it:

Del handler = new Del( DelegateMethod);
handler("Hello World");
Console.Read();

Why when I drop the static from the DelegateMethod do I get an error?

Why does the function I delegate have to be static?

Upvotes: 0

Views: 397

Answers (5)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

why when i drop the "static" from the DelegateMethod i get an error?

Because you are writing this code inside a static method. Given the fact that you are using Console.Read I presume you have put this code inside the static void Main method of your console application. If you wanted to drop the static keyword from the method you will need an instance of the class containing this method. Like this:

class Program
{
    public delegate void Del(string message);

    public void DelegateMethod(string message)
    {
        System.Console.WriteLine(message);
    }

    static void Main()
    {
        Del handler = new Del(new Program().DelegateMethod);
        handler("Hello World");
        Console.Read();
    }
}

Upvotes: 3

Stilgar
Stilgar

Reputation: 23561

It doesn't need to be static but a non-static method needs an instance. This is the very definition of non-static (instance) method. You can pass an instance method (and the instance) to a delegate like this:

Del handler = new Del(instanceVariable.DelegateMethod);

You can add instance methods to delegates in other instance method of the same class. It is assumed that the instance is this.

Keep in mind that in this way the instance will not be eligible for GC as long as the delegate instance lives. This is sometimes a reason for object leaks (some people call them memory leaks despite the fact that the leak is on a higher level and is different from C-style memory leak)

Upvotes: 1

Oded
Oded

Reputation: 499062

You are mistaken about delegates needing to use static methods.

A method signature (return type and parameters - types, number and order) needs to match a delegate in order to be compatible with it.

From MSDN - Delegates (C# Programming Guide):

Any method from any accessible class or struct that matches the delegate's signature, which consists of the return type and parameters, can be assigned to the delegate. The method can be either static or an instance method.

(emphasis mine)


When using an instance method as the delegate target, you must refer to an actual instance:

public class MyClass
{
    public static void DelegateMethod(string message)
    {
        System.Console.WriteLine(message);
    }
}

var myClass = new MyClass();
Del handler = new Del(myClass.DelegateMethod);

Upvotes: 1

jason
jason

Reputation: 241651

why when i drop the "static" from the DelegateMethod i get an error?

You didn't specify exactly what error you're confused by. I suspect the error you're confused by is the fact that this line will not compile if DelegateMethod is not static and you are writing the line of code in a static method in the containing class:

Del handler = new Del( DelegateMethod); 

The reason for this is because if you don't declare DelegateMethod as static, then you need an instance to refer to the method. Given that your code is likely being written in a static method for the containing class, there is no implicit this and thus you need an explicit instance. Assuming your containing class is named Foo:

Foo foo = new Foo();
Del handler = ne Del(foo.DelegateMethod);

See?

Upvotes: 1

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60694

Just a guess here. Is the method that contains this code:

Del handler = new Del( DelegateMethod);
handler("Hello World");
Console.Read();

Also static? In that case, DelegateMethod must be static as well, since you can't reference a non-static method from a static one.

Upvotes: 0

Related Questions