简体   繁体   English

一个类中的委托是什么意思?

[英]What is the meaning of a delegate inside a class?

I understand delegates as a shortcut for defining a class with one method but what is the meaning of bar2 below? 我理解委托是用一种方法定义一个类的快捷方式,但下面的bar2是什么意思? It compiles. 它汇编。 But I can't see what an inner class would do here. 但我看不出内心阶级会做什么。 I know I'm missing something so that's why I'm asking (this is not homework, I'm at work-work right now). 我知道我错过了什么,这就是为什么我要问(这不是作业,我现在正在工作)。

namespace ns2 { public delegate void bar();}
public class foo
{
    private ns2.bar _callback;
    public foo(ns2.bar callback) { _callback = callback; }
    public void baz() { _callback(); }
    public delegate void bar2();
}

Thanks! 谢谢!

To add to Jon and Jared's answers, I note that the only time you'd usually define a delegate inside a class is if the delegate were a private implementation detail of the class. 为了添加Jon和Jared的答案,我注意到你通常在一个类中定义一个委托的唯一时间是委托是该类的私有实现细节。 It is rare and bizarre to have a public delegate definition inside a class. 在类中包含公共委托定义是罕见且奇怪的。

For example, consider my favourite pattern: an abstract base class that can only be extended by private nested classes that are manufactured with factories: 例如,考虑我最喜欢的模式:一个抽象基类,只能通过使用工厂制造的私有嵌套类进行扩展:

public abstract class Animal
{
    private Animal() { } // prevents subclassing from outside!
    private sealed class Giraffe : Animal { }
    private sealed class Leopard : Animal { }
    public static Animal GetGiraffe( ) { return new Giraffe(); }
    public static Animal GetLeopard( ) { return new Leopard(); }

Suppose an implementation detail of Animal was that you needed to have a delegate from Giraffe to Leopard: 假设Animal的实现细节是你需要一个从长颈鹿到Leopard的代表:

    private delegate Leopard D(Giraffe g);

This cannot be a public delegate class because it refers to private types! 这不能是公共委托类,因为它引用了私有类型!

Nowadays of course you wouldn't even do this. 现在你当然不会这样做。 You'd use Func<Giraffe, Leopard> and you're done. 你会使用Func<Giraffe, Leopard>而且你已经完成了。

Therefore, the nested delegate type is pretty much there for completeness and backwards compatibility these days; 因此,嵌套委托类型几乎就是为了完整性和向后兼容性; normally you wouldn't use it. 通常你不会使用它。

Delegates are not shortcuts for defining a class with one method. 委托不是使用一种方法定义类的快捷方式。 They in fact have at a minimum 6 methods (Invoke, BeginInvoke, EndInvoke, GetHashCode, Equals, ToString). 实际上它们至少有6个方法(Invoke,BeginInvoke,EndInvoke,GetHashCode,Equals,ToString)。

One reason for the pattern of putting delegates inside a type is to avoid global namespace pollution. 将委托放在类型中的模式的一个原因是避免全局命名空间污染。 The delegate bar2 is only accessible through a qualified reference from foo . 代表bar2只能通过foo的限定引用访问。 This can be valuable if bar2 is a name which has multiple valid meannings and putting it within foo provides necessary context. 如果bar2是具有多个有效bar2的名称并且将其置于foo内提供必要的上下文,则这可能是有价值的。

It's just declaring a nested type, that's all. 它只是声明一个嵌套类型,就是这样。 As it's public, there's pretty much no difference between that being declared inside the type and being declared outside. 因为它是公开的,所以在类型内声明和在外面声明之间几乎没有区别。

Other classes will have to refer to it via the name of the containing type (unless they have a using directive specifically for it): 其他类必须通过包含类型的名称引用它(除非它们具有专门用于它的using指令):

public class OtherClass
{
    private void DoSomething()
    {
        foo.bar2 action = delegate { ... };
        foo f = new foo(action);
        ...
    }
}

Usually when I write a nested type, it's private - it's just a helper class for the containing class; 通常当我编写嵌套类型时,它是私有的 - 它只是包含类的辅助类; an implementation detail. 一个实现细节。 That doesn't have to be the case, of course, but it should at least be true that the nested type is meaningless unless you're in some way using the outer class. 当然,情况并非必须如此,但至少应该是嵌套类型没有意义,除非你以某种方式使用外部类。 For example, it could be a builder for the outer class, or an enum used in some parameters for a method call within the outer class. 例如,它可以是外部类的构建器,也可以是外部类中方法调用的某些参数中使用的枚举。

(By the way, it's useful when writing sample code like this to follow .NET naming conventions even for meta-syntactic names like Foo and Bar . It makes the difference between variables and types clearer, for example.) (顺便说一句,在编写像这样的示例代码时,即使对于FooBar这样的元语法名称,它也很有用。例如,它使变量和类型之间的区别更加清晰。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM