Why use “new DelegateType(Delegate)”?

前端 未结 5 1088
时光取名叫无心
时光取名叫无心 2020-12-06 07:15

Ok, suppose you define a delegate in some class.

public delegate void StringDelegate (string s);

and another class implements a method :

5条回答
  •  广开言路
    2020-12-06 07:40

    There is absolutely no difference between the two statements. writer = DelegateImplementer.StringWriter; still creates a delegate object; the compiler will generate the new ClassDelegate.StringDelegate () for you. It's just a cleaner syntax that was added in C# 2.0.

    As @Ben Voigt mentioned in his answer is only required in C# 2.0 where the compiler can't deduce the type of the delegate, when using Control.Invoke() for example.

提交回复
热议问题