棄元就是使用下劃線_作為一個占位符,但不占存儲空間。 元組(ValueTuple、Tuple)使用棄元例子。 using System; namespace ConsoleApp4 { class Program { public static void Main() { // ValueTuple ...
棄元就是使用下劃線_作為一個占位符,但不占存儲空間。
元組(ValueTuple、Tuple)使用棄元例子。

using System; namespace ConsoleApp4 { class Program { public static void Main() { // ValueTuple和Tuple都可以使用棄元 var (_, _, _, pop1, _, pop2) = QueryCityDataForYears("New York City", 1960, 2010); Console.WriteLine($"Population change, 1960 to 2010: {pop2 - pop1:N0}"); } private static (string,double,int,int,int,int) QueryCityDataForYears(string name,int year1,int year2) { int population1 = 0, population2 = 0; double area = 0; if (name == "New York City") { area = 468.48; if (year1 == 1960) { population1 = 7781984; } if (year2 == 2010) { population2 = 8175133; } return (name, area, year1, population1, year2, population2); } return ("", 0, 0, 0, 0, 0); } } }View Code
對象析構,析構函數的參數決定了元組的參數 。

using System; namespace ConsoleApp4 { class Program { public static void Main() { var p = new Person("John", "Quincy", "Adams", "Boston", "MA"); // Deconstruct(out string fname, out string lname, out string city, out string state) var (fName,_,city,_) = p; Console.WriteLine($"Hello {fName} of {city}!"); } private static (string,double,int,int,int,int) QueryCityDataForYears(string name,int year1,int year2) { int population1 = 0, population2 = 0; double area = 0; if (name == "New York City") { area = 468.48; if (year1 == 1960) { population1 = 7781984; } if (year2 == 2010) { population2 = 8175133; } return (name, area, year1, population1, year2, population2); } return ("", 0, 0, 0, 0, 0); } } public class Person { public string FirstName { get; set; } public string MiddleName { get; set; } public string LastName { get; set; } public string City { get; set; } public string State { get; set; } public Person(string fname, string mname, string lname, string cityName, string stateName) { FirstName = fname; MiddleName = mname; LastName = lname; City = cityName; State = stateName; } // 返回FirstName、LastName的元組 public void Deconstruct(out string fname,out string lname) { fname = FirstName; lname = LastName; } public void Deconstruct(out string fname, out string mname, out string lname) { fname = FirstName; mname = MiddleName; lname = LastName; } public void Deconstruct(out string fname, out string lname, out string city, out string state) { fname = FirstName; lname = LastName; city = City; state = State; } } }View Code
switch,case分支可以使用占位符,相當default的作用。

using System; using System.Globalization; namespace ConsoleApp4 { class Program { public static void Main() { object[] objects = { CultureInfo.CurrentCulture, CultureInfo.CurrentCulture.DateTimeFormat, CultureInfo.CurrentCulture.NumberFormat, new ArgumentException(), null }; foreach (var obj in objects) ProvidesFormatInfo(obj); Console.ReadLine(); } private static void ProvidesFormatInfo(object obj) { switch (obj) { // 實現了IFormatProvider,if obj is IFormatProvider fmt case IFormatProvider fmt: Console.WriteLine($"{fmt} object"); break; case null: Console.WriteLine("null"); break; case object _: Console.WriteLine("without format information"); break; } } } }View Code
具有out參數的調用方法,有時只想知道是否能轉換而不需要其轉換之後的值。
bool isDate = DateTime.TryParse("8989", out _);
獨立的棄元 (好像沒有用....)
_ = "";
當上下文存在下劃線標識符,則無法再用下劃線作為占位符,即不能使用棄元。
private static void ShowValue(int _) { byte[] arr = { 0, 0, 1, 2 }; // 此時無法作為占位符,因為和參數的有效標識符衝突 _ = BitConverter.ToInt32(arr, 0); Console.WriteLine(_); } // The example displays the following output: // 33619968
private static void ShowValue(int _) { string value = _.ToString(); int newValue = 0; // 無法作為占位符,類型和參數標識符_也不一樣,編譯報錯 _ = Int32.TryParse(value, out newValue); return _ == newValue; }