弃元就是使用下划线_作为一个占位符,但不占存储空间。
元组(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); } } }
对象析构,析构函数的参数决定了元组的参数 。
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; } } }
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; } } } }
具有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; }