为什么新的Thread()接受一个方法名,尽管没有一个构造函数重载似乎允许这样做? [英] Why does new Thread() accept a method name, even though none of the constructor overloads seem to allow this?

查看:153
本文介绍了为什么新的Thread()接受一个方法名,尽管没有一个构造函数重载似乎允许这样做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/10482121/what-is-the-difference-between-new-threadvoid-target-and-new-threadnew-thre">What是新的线程之间的差值(空目标())和新主题(新的ThreadStart(空目标()))?

我对Thread类的小问题。这个类有4构造函数:

I have a small question about Thread class. This class has 4 constructors:

public Thread(ParameterizedThreadStart start);
public Thread(ThreadStart start);
public Thread(ParameterizedThreadStart start, int maxStackSize);
public Thread(ThreadStart start, int maxStackSize);

我用的是第二个构造函数来创建Thread对象:

I use the 2nd constructor to create a Thread object:

Thread thread = new Thread(new ThreadStart(ScanDirectory));

不过,我可以用一种方式来创建这个对象,而无需使用我讲上面的任何构造函数。

However, I can use a way to create this object without using any constructors I talk above.

Thread thread = new Thread(ScanDirectory);

在这种情况下,ScanDirectory是一个无效的方法,它不是的ThreadStart或ParameterizedThreadStart但Thread类还是接受这个构造。为什么? 我认为这是一个.NET功能,但我不知道它是如何实现的。

In this case, ScanDirectory is a void method, it isn't ThreadStart or ParameterizedThreadStart but Thread class still accepts this constructor. Why? I think this is a .NET feature but I don't know how it's implemented.

注意: ScanDirectory是一个无效的方法

Note: ScanDirectory is a void method.

推荐答案

据分开两件事很重要:

  • 线程调用构造
  • 为代表的创造的通过的到构造
  • The call to the Thread constructor
  • The creation of a delegate to pass to the Thread constructor

你的真正的兴趣,后者在这里 - 之间的区别:

You're really interested in the latter here - the difference between:

ThreadStart tmp = new ThreadStart(ScanDirectory);

ThreadStart tmp = ScanDirectory;

这些第二种是的方法组转换的 - 从方法组的(一个方法的名称,可能受限定的通过一个实例值,如果它是一个隐式转换实例方法)与兼容签名的委托。

The second of these is a method group conversion - an implicit conversion from a method group (the name of a method, possibly qualified by an instance value if it's an instance method) to a delegate with a compatible signature.

您很少需要明确的委托创作EX $ P $第一种形式的pssion,由于方法组转换是在C#2,介绍你会看到很多code仍然使用它,因为很多开发者没有注意到方法组转换,遗憾的是 - 和IIRC Visual Studio的设计师仍然使用这种形式的事件处理程序订阅

You very rarely need the "explicit" delegate creation expression of the first form, since method group conversions were introduced in C# 2. You'll see a lot of code which still uses it because many developers are unaware of method group conversions, unfortunately - and IIRC the Visual Studio designers still uses that form for event handler subscription.

你真正需要的时候,方法组转换结束了模棱两可这是唯一的一次。例如:

The only time you'd really need it was when the method group conversion ended up being ambiguous. For example:

static void Main()
{
    // Valid: uses parameterless overload
    new Thread(new ThreadStart(Foo));

    // Valid: uses parameterized overload
    new Thread(new ParameterizedThreadStart(Foo));

    // Invalid, as there are two valid constructor overloads
    new Thread(Foo);
}

static void Foo()
{
}

static void Foo(object state)
{
}

这篇关于为什么新的Thread()接受一个方法名,尽管没有一个构造函数重载似乎允许这样做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆