C# 为什么委托在静态方法中使用时不能引用非静态方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2298997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:12:25  来源:igfitidea点击:

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

c#.netvisual-studio-2005functiondelegates

提问by Sundhas

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

为什么在 C# 中使用委托时需要使函数 STATIC ?

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;
    }
}

采纳答案by Dаn

It's not "necessary". But your Mainmethod is static, so it can't call a non-staticmethod. 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):

这不是必需的”。但是你的Main方法是static,所以它不能调用非static方法。尝试这样的事情(这不是一个很好的做事方式——你真的应该创建一个新类,但它不会改变你的样本):

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; 
    } 
}

回答by Amy

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

您的函数需要是静态的,因为您是从静态方法 Main 调用的。您可以使方法非静态:

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;
    }
}

回答by tvanfosson

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.

在这种情况下,因为您没有创建任何类的实例,唯一的替代方法是静态函数。如果要实例化 Program 类型的对象,则可以改用实例方法。

回答by sirchristian

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.

您可以使用具有非静态方法的委托。

回答by Rocky

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)

DelegateName DN = new DelegateName(类的实例。方法名)