【问题标题】:C# What does the => syntax mean [duplicate]C# => 语法是什么意思
【发布时间】:2016-09-17 06:43:17
【问题描述】:

我正在尝试理解 C#,但对这段代码感到非常困惑。

Thread thread = new Thread(new ThreadStart(() =>
{
       Thread.Sleep(_rnd.Next(50, 200));
       //do other stuff
}));
thread.Start();

我知道正在创建一个线程然后启动,但在这种情况下我不理解 => 语法。通过检查该网站上的其他帖子,这些帖子很难找到关于 => 的内容,我认为这与代表有关,或者正在返回某些东西?任何人都可以对此有所了解吗?谢谢。

【问题讨论】:

  • 这是一个delegate 速记语法

标签: c#


【解决方案1】:

您将在下面找到一些使用函数和委托的不同方法。所有人都做同样的事情:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SO39543690
{
  class Program
  {
    static Random _rnd = new Random();

    static void Proc()
    {
      Console.WriteLine("3. Processing...");
      Thread.Sleep(_rnd.Next(50, 200));
    }

    static void Main(string[] args)
    {
      Thread thread = new Thread(new ThreadStart(() =>
      {
        Console.WriteLine("1. Processing...");
        Thread.Sleep(_rnd.Next(50, 200));
      }));
      thread.Start();

      thread = new Thread(() =>
      {
        Console.WriteLine("2. Processing...");
        Thread.Sleep(_rnd.Next(50, 200));
      });
      thread.Start();

      thread = new Thread(Proc);
      thread.Start();

      thread = new Thread(delegate ()
      {
        Console.WriteLine("4. Processing...");
        Thread.Sleep(_rnd.Next(50, 200));
      });
      thread.Start();

      Action proc = () =>
      {
        Console.WriteLine("5. Processing...");
        Thread.Sleep(_rnd.Next(50, 200));
      };
      thread = new Thread(new ThreadStart(proc));
      thread.Start();


      Console.WriteLine("END");
      Console.ReadLine();
    }
  }
}

【讨论】:

    猜你喜欢
    • 2018-05-12
    • 1970-01-01
    • 2017-04-13
    • 2010-09-22
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 2011-08-01
    相关资源
    最近更新 更多