nameof运算式

运算式nameof可以取得变数/常数、命名空间、类型或成员的名称作为字串表示形式:

static void Main(string[] args){    var name = "Bill";    var age = 18;    Console.WriteLine(nameof(System.Collections.Generic));  // output: Generic    Console.WriteLine(nameof(List<int>));  // output: List    Console.WriteLine(nameof(List<int>.Count));  // output: Count    Console.WriteLine(nameof(List<int>.Add));  // output: Add    Console.WriteLine(nameof(name));  // output: name    Console.WriteLine(nameof(age));  // output: age}

在型别与命名空间的情况下,产生的名称通常是不完整的,以上述範例为例,System.Collections.Generic命名空间,会显示Generic
http://img2.58codes.com/2024/20112573V4Dkhsmx4r.png


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章