Background:
Determine if a variable is a format in ". Txt.doc.xls.ppt.pdf", and if so, perform the appropriate action.
Method One: Use generics
ReadOnly ilist<string> fnames = new list<string> () { ". doc", ". txt", " . xls", ". ppt", ". pdf" }; private void Test (string fName) { if (Fnames.contains (FName)) { MessageBox.Show (fName); }}
Method two: Use LINQ
private void Test (string fName) { if (new string[] {". doc", ". txt", ". xls"}. Any (x = FName = = x)) { MessageBox.Show (fName); }}
If it's &&, use all
Knowledge Development:
LINQ query syntax
Lambda expression-expression tree
Examples of simple usages of LINQ queries
Method Three: Use the index function
private void Test (string fName) { string str = ". Txt.doc.xls.ppt.pdf"; if (str. IndexOf (FName) >= 0) { MessageBox.Show (fName); }}
C # IndexOf Usage
The above is the content of C # using LINQ, Generics, and index functions to optimize the switch (or multiple if) statements, and more about topic.alibabacloud.com (www.php.cn)!