棄元就是使用下劃線_作為一個占位符,但不占存盤空間,
元組(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 =https://www.cnblogs.com/bibi-feiniaoyuan/p/ _.ToString(); int newValue = https://www.cnblogs.com/bibi-feiniaoyuan/p/0; // 無法作為占位符,型別和引數識別符號_也不一樣,編譯報錯 _ = Int32.TryParse(value, out newValue); return _ == newValue; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/61585.html
標籤:C#
上一篇:c# 委托的進化
下一篇:C#基礎篇——反射
