C#=>是什么语法意思

时间:2016-09-17 06:43:18

标签: c#

我试图理解C#并且对这段代码非常困惑。

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

我知道正在创建一个帖子然后开始但我不理解=>这种情况下的语法。从查看本网站上很难找到关于=>的其他帖子,我认为这是与代表有关或者有什么东西被退回?任何人都可以对此有所了解吗?谢谢。

1 个答案:

答案 0 :(得分: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();
    }
  }
}