c# - Can I turn this into a lambda statement? -


i wondering if it's possible turn while loop lambda statement? know it's possible if or foreach loop, it's normal, plain while loop:

while (path.substring(path.length - 4) != ".txt" || path.substring(path.length - 4) != ".xml") {     console.writeline("file not .txt or .xml extension! enter file name:");     path = console.readline(); } 

if possible, how 1 transform loop such lambda statement?

given comments on question suggest isn't lambdas, minimizing code, here small suggestions avoid code duplication:

string[] validextensions = { ".txt", ".xml" }; {     console.writeline("enter file name:");     path = console.readline();     if (!validextensions.contains(path.getextension(path)))     {         console.write("file not .txt or .xml extension! ");         path = null;     } } while (path == null); 

checking extension merely requires adding extension array, doesn't require duplicating code determine extension. string "enter file name:" has appear once, if want different message first prompt. code read line has appear once.

personally, i'd duplication had small there no need avoid yet, may find useful if, example, need allow 3 more extensions, or read other location single function call not suffice.

some additional comments:

  • console.readline() can return null. code in question, version doesn't handle properly.
  • case in file extensions ignored. want reject ".txt" file extension?
  • your while condition path.substring(path.length - 4) != ".txt" || path.substring(path.length - 4) != ".xml" never false. true, or throw exception, loop never terminate normally.

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -