C#8.0言語推奨

6041 ワード

参考文献:https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/proposals/csharp-8.0/nullable-reference-types
例:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ConsoleApp8
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var student = new Student
            {
                Name = "A",
                Sex = "B"
            };

            if (student is Student)
            {
                //  
            }

            if (student.Equals(new Student { Name = "A", Sex = "B" }))
            {
                //   
            }

            if (student == new Student { Name = "A", Sex = "B" })
            {
                //   
            }

            //
            if (student is Student { Name: "A", Sex: "B" } stdValue)
            {
                //  
                Console.WriteLine(stdValue.Name);
            }

            //      IAsyncEnumerable    IAsyncEnumerator     
            var resultEnumerable = GenerateSequence().GetAsyncEnumerator();
            while (await resultEnumerable.MoveNextAsync())
            {
                Console.WriteLine(resultEnumerable.Current);
            }


            //Switch Expression
            var realName =
              (student.Name, student.Sex, student.Age) switch
              {
                  ("zhangsan", "boy", _) => "good boy",
                  ("A", "B", 0) => "good boy",
                  (_, _, _) => string.Empty
              };
            Console.WriteLine(realName);

            //      using   using   
            using var memory = new System.IO.MemoryStream();
            var buff = System.Text.Encoding.UTF8.GetBytes("hi");
            await memory.WriteAsync(buff, 0, buff.Length);
            using var memory2 = new System.IO.MemoryStream();
            buff = System.Text.Encoding.UTF8.GetBytes("hi");
            await memory2.WriteAsync(buff, 0, buff.Length);

          

            Console.WriteLine("Hello World!");

            Console.ReadKey();
        }

        static async IAsyncEnumerable GenerateSequence()
        {
            for (int i = 0; i < 20; i++)
            {
                await Task.Delay(100);
                yield return i;
            }
        }

    }

    public class Student
    {
        /// 
        /// 
        /// 

        public string Name { get; set; }

        /// 
        /// 
        /// 
        public string Sex { get; set; }

        /// 
        /// 
        /// 
        public int Age { get; set; }

        /// 
        /// 
        /// 
        public DateTime BirthDay { get; set; }
    }


}

逆コンパイル:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp8
{
	// Token: 0x02000002 RID: 2
	internal class Program
	{
		// Token: 0x06000001 RID: 1 RVA: 0x00002050 File Offset: 0x00000250
		private static async Task Main(string[] args)
		{
			Student student = new Student
			{
				Name = "A",
				Sex = "B"
			};
			bool flag = student != null;
			if (flag)
			{
			}
			bool flag2 = student.Equals(new Student
			{
				Name = "A",
				Sex = "B"
			});
			if (flag2)
			{
			}
			bool flag3 = student == new Student
			{
				Name = "A",
				Sex = "B"
			};
			if (flag3)
			{
			}
			string text;
			string text2;
			Student stdValue;
			bool flag4;
			if (student != null)
			{
				text = student.Name;
				if (text != null && text == "A")
				{
					text2 = student.Sex;
					if (text2 != null && text2 == "B")
					{
						stdValue = student;
						flag4 = true;
						goto IL_100;
					}
				}
			}
			flag4 = false;
			IL_100:
			bool flag5 = flag4;
			if (flag5)
			{
				Console.WriteLine(stdValue.Name);
			}
			IAsyncEnumerator resultEnumerable = Program.GenerateSequence().GetAsyncEnumerator(default(CancellationToken));
			while (await resultEnumerable.MoveNextAsync())
			{
				Console.WriteLine(resultEnumerable.Current);
			}
			int age = student.Age;
			text = student.Sex;
			string name = student.Name;
			if (name != null)
			{
				if (!(name == "zhangsan"))
				{
					if (name == "A")
					{
						if (text != null && text == "B")
						{
							if (age == 0)
							{
								text2 = "good boy";
								goto IL_259;
							}
						}
					}
				}
				else if (text != null)
				{
					if (text == "boy")
					{
						text2 = "good boy";
						goto IL_259;
					}
				}
			}
			text2 = string.Empty;
			IL_259:
			string text3 = text2;
			string realName = text3;
			text3 = null;
			Console.WriteLine(realName);
			using (MemoryStream memory = new MemoryStream())
			{
				byte[] buff = Encoding.UTF8.GetBytes("hi");
				await memory.WriteAsync(buff, 0, buff.Length);
				using (MemoryStream memory2 = new MemoryStream())
				{
					buff = Encoding.UTF8.GetBytes("hi");
					await memory2.WriteAsync(buff, 0, buff.Length);
					Console.WriteLine("Hello World!");
					Console.ReadKey();
				}
			}
		}

		// Token: 0x06000002 RID: 2 RVA: 0x00002097 File Offset: 0x00000297
		[AsyncIteratorStateMachine(typeof(Program.d__1))]
		private static IAsyncEnumerable GenerateSequence()
		{
			return new Program.d__1(-2);
		}

		// Token: 0x06000004 RID: 4 RVA: 0x000020AC File Offset: 0x000002AC
		[DebuggerStepThrough]
		private static void 
(string[] args) { Program.Main(args).GetAwaiter().GetResult(); } } }