很简单的委托沉思

很简单的委托沉思

本文介绍了很简单的委托沉思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候最简单的问题让我爱C / C ++和C#也越来越多。



今天坐在公交车上沉思AOUT代表我想起
阅读somwhere你不需要instaniating
A新的委托时使用new关键字



例如:

 公共静态无效的someMethod(字符串消息)
{

}

...


酒店的公共委托无效TestDelgate(字符串消息); //定义委托

...........

//创建一个新的实例..METHOD 1
TestDelgate T =新TestDelgate(的someMethod);

//或另一种方式来创建一个新的实例..METHOD 2
TestDelgate T =的someMethod; //创建一个新的实例..METHOD 2



所以,今天的问题是




  1. 引擎盖下发生在方法2什么是编译器扩展方法2到方法1,因此写TestDelgate T =的someMethod;仅仅是
    TestDelgate T =快捷方式的新TestDelgate(的someMethod),或有另一种原因是方法2的exsitence


  2. 你你们认为方法1或方法2是可读性更好(这是主观的问题,但我只是想拿到计算器一般看法不同的不科学的感觉: - ))



解决方案

有糖。那种糖,真正派上用场的时候,你必须这样写:

  someObj.Test  -  =新TestDelegate(的someMethod) ; 



什么?你必须使用的新的的关键字的删除的事件处理程序。是的你是。在VB.NET球队真正心疼这一点,他们决定一个完全不同的语法:

  RemoveHandler someObj.Test,AddressOf $的someMethod b $ b  



即使在上述C#语句是糖,真正的代码看起来像这样

  someObj.Test.remove(新TestDelegate(这一点,的someMethod)); 



其中,删除是一个事件的访问功能。和这是必需的初始化Delegate.Target属性。现在很明显,它实际上是一个方法调用,并使用new关键字又突然很有意义。藏这种有一些缺点太,它并不明显了该事件订阅将防止从得到垃圾收集的对象。


Sometimes the simplest questions make me love C/C++ and C# more and more.

Today sitting on the bus musing aout delegates I rememberedreading somwhere you don't need to use the new keyword when instaniatinga new delegate.

For example:

    public static void SomeMethod(string message)
    {
      ...
    }

    ...


   public delegate void TestDelgate(string message); //Define a delegate

    ...........

   //create a new instance ..METHOD 1
   TestDelgate t = new TestDelgate(SomeMethod);

   //OR another way to create a new instance ..METHOD 2
   TestDelgate t = SomeMethod;   //create a new instance ..METHOD 2

So todays questions are

  1. What happens under the hood in method 2. Does the compiler expand method 2 into method 1, hence writing TestDelgate t = SomeMethod; is just a shortcut forTestDelgate t = new TestDelgate(SomeMethod);, or is there another reason for the exsitence of method 2

  2. Do you guys think method 1 or method 2 is better for readability (this is a subjective question, but I'd just like to get a unscientific feel of general opinion of stackoverflow :-))

解决方案

It is sugar. The kind of sugar that really comes in handy when you have to write this:

 someObj.Test -= new TestDelegate(SomeMethod);

What? You have to use the new keyword to remove an event handler. Yes, you do. The VB.NET team really pained about this, they decided for a completely different syntax:

 RemoveHandler someObj.Test, AddressOf SomeMethod


Even the above C# statement is sugar, the real code looks like this:

 someObj.Test.remove(new TestDelegate(this, SomeMethod));

Where "remove" is the accessor function for an event. And "this" is required to initialize the Delegate.Target property. Now it is obvious that it is actually a method call and using the new keyword suddenly makes sense again. Hiding "this" has some disadvantages too, it isn't obvious anymore that an event subscription will prevent an object from getting garbage collected.

这篇关于很简单的委托沉思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:58