C# 6.0 的知识梳理

2019-12-30 15:27:01于丽


class MyClass
 {
 public int this[int id] => id; //索引
 public double Add(int x, int y) => x + y; //带返回值方法
 public void Output() => Console.WriteLine("Hi, Fanguzai!"); //无返回值方法
 }

九、索引初始值设定项

现在可以初始化支持索引编制的集合的特定元素(如初始化字典)。如果集合支持索引,可以指定索引元素。


var nums = new Dictionary<int, string>
 {
 [7] = "seven",
 [9] = "nine",
 [13] = "thirteen"
 };
 //这是旧的方式
 var otherNums = new Dictionary<int, string>()
 {
 {1, "one"},
 {2, "two"},
 {3, "three"}
 };

十、using static 类型

可以导入静态类型的可访问静态成员,以便可以在不使用类型名限定访问的情况下引用成员。


using System;
using static System.Console;
namespace _08_usingStatic类型
{
 class Program
 {
 static void Main(string[] args)
 {
 Console.WriteLine("Hi,Fanguzai!");
 WriteLine("Hi,Fanguzai!"); // 使用了 using static System.Console;
 }
 }
}

using static 仅导入可访问的静态成员和指定类型中声明的嵌套类型,不会导入继承的成员。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持ASPKU!


注:相关教程知识阅读请移步到c#教程频道。