iT邦幫忙

0

nameof運算式

c#
WM 2020-04-12 13:49:15840 瀏覽

運算式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
https://ithelp.ithome.com.tw/upload/images/20200412/20112573V4Dkhsmx4r.png


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言