C#6 语法特性

102 阅读2分钟

一、静态导入

1.1 静态属性、静态方法、枚举 示例

namespace c_6_1
{
    internal class MyStaticClass
    {
        public static int id { get; set; } = 1;

        public static string name;

        public static void StaticMethod()
        {
            Console.WriteLine("Do...");
        }

        public static void StaticMethodWithExcption() {
            throw new NotImplementedException();
        }

    }

    enum Color
    {
        Red,
        Green,
        Blue,
    }
}

1.2 原调用

using c_6_1;

Console.WriteLine(Color.Red);

Console.WriteLine(MyStaticClass.id);
Console.WriteLine(MyStaticClass.name);
MyStaticClass.StaticMethod();
MyStaticClass.StaticMethodWithExcption();

1.3 静态调用

using static c_6_1.Color;
using static c_6_1.MyStaticClass;

Console.WriteLine(Red);

Console.WriteLine(id);
Console.WriteLine(name);
StaticMethod();
StaticMethodWithExcption();

二、Using 别名

2.1 创建复杂的命名空间和子实体类

namespace a.b.c.d.e
{
    internal class CommonUtils
    {
        public static void Print()
        {
            Console.WriteLine("Hello world");
        }
    }
}

2.2 原调用

using a.b.c.d.e;

CommonUtils.Print();

2.3 使用 Using 别名

using utils = a.b.c.d.e.CommonUtils;

utils.Print();

2.4 使用案例

2.4.1 创建泛型类

namespace a.b.c.d.e
{
    internal class GenericClass<T>
    {
        public void Show(T t)
        {
            Console.WriteLine($"T name is {typeof(T).FullName}");
        }
    }
}

2.4.2 指定特定泛型

using genericeInt = a.b.c.d.e.GenericClass<int>;
using genericeString = a.b.c.d.e.GenericClass<string>;

new genericeInt().Show(123);
new genericeString().Show("123");

2.4.3 运行结果

T name is System.Int32
T name is System.String

Z:\git_tst\.NET\c#6_1\c#6_1\bin\Debug\net8.0\c#6_1.exe (进程 25548)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .

三、 异常条件过滤

3.1 创建异常抛出类

namespace c_6_1
{
    internal class ThrowMyException
    {
        public void Throw() { 
        
            // throw new Exception("001");
            throw new Exception("002");
        }
    }
}

3.2 抛出异常

using c_6_1;

ThrowMyException throwMyException = new ThrowMyException();

try
{
    throwMyException.Throw();
}catch(Exception ex) when(ex.Message.Contains("002"))
{
    Console.WriteLine("捕捉到002");
}

由于指定了 when(ex.Message.Contains("002"),只会捕捉Message中包含'002'的异常

image.png

若放开注释异常001,则无法捕捉

image.png

四、 自动初始化表达式

4.1 属性自动赋值

internal class Student
{
    public int Id { get; set; } = 1;
}
Console.WriteLine(new Student().Id);
image.png

4.2 简化实体类方法

 internal class Student
 {
     public int Id { get; set; } = 1;

     public string Name { get; set; } = "xyy";

     // 原来写法
     public int GetStuId()
     {
         return Id;
     }
     // 简化写法
     public string GetStuName() => this.Name;
 }

五、Null 传播器

5.1 创建实体类

    internal class Student
    {
        public int Id { get; set; } = 24;

        public string Name { get; set; } = "xyy";

    }

5.2 原处理实体类

在调用 Student 类时,会提前对其进行非空判断

Student stu = null;

if (stu != null)
{
    // Handle ....
}

若不进行判断,当实体类为空且调用其属性时,将会爆:

System.NullReferenceException:“Object reference not set to an instance of an object.”

5.3 使用 ?运算符

在调用属性时,加上即可对 null值 做出处理

image.png

六、 nameof 运算符

// nameof 表达式
Console.WriteLine(nameof(System.Collections.Generic)); //Generic
Console.WriteLine(nameof(List<int>));                   // List
Console.WriteLine(nameof(List<int>.Count));             // Count
Console.WriteLine(nameof(List<int>.Add));               // Add

List<int> menbers = new List<int> { 1, 2, 3 };
Console.WriteLine(nameof(menbers));                     // menbers
Console.WriteLine(nameof(menbers.Count));               // Count
Console.WriteLine(nameof(menbers.Add));                 // Add

Console.WriteLine();

image.png