gpt4 book ai didi

c# - 从 LinqPad 中的类运行

转载 作者:行者123 更新时间:2023-12-04 02:08:36 26 4
gpt4 key购买 nike

如何在 LinqPad 中将下面的代码作为 C# 程序运行,谢谢...

class ThreadTest
{
bool done;

static void Main()
{
ThreadTest tt = new ThreadTest(); // Create a common instance
new Thread (tt.Go).Start();
tt.Go();
}

// Note that Go is now an instance method
void Go()
{
if (!done) { done = true; Console.WriteLine ("Done"); }
}
}

UPDATE

This sample - along with all the others in the concurrency chapters of C# 5 in a Nutshell are downloadable as a LINQPad sample library. Go to LINQPad's samples TreeView and click 'Download/Import more samples' and choose the first listing. – Joe Albahari

最佳答案

只需将 Main 移出 ThreadTest,它应该可以正常工作。您还需要将类和方法设为public(或此时为internal):

static void Main()
{
ThreadTest tt = new ThreadTest(); // Create a common instance
new Thread (tt.Go).Start();
tt.Go();
}

public class ThreadTest
{
bool done;
// Note that Go is now an instance method
public void Go()
{
if (!done) { done = true; Console.WriteLine ("Done"); }
}
}

“C# 程序”隐式包含在一个类中 - 将 main 移动到嵌套类中可能会使正在最外层类中寻找 Main 的执行程序感到困惑。

关于c# - 从 LinqPad 中的类运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21460619/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com