I have a question. Consider that scenerio:
public override string Encrypt(string input)
{
Func<char, char> encryption = (c) => (char)chars[(c * k1 + k0) % 26];
string result = string.Empty;
foreach(char c in input)
result += encryption(c);
return result;
}
My question is, can we change that result += encryption(c)
line into something like in Func<char, char>
declaration? Can we write this anonymous method in one line ?
您可以执行以下操作:
result = string.Concat(input.Select(c => encryption(c)))
You can try using Linq , ie
public override string Encrypt(string input)
{
Func<char, char> encryption = (c) => (char)chars[(c * k1 + k0) % 26];
return string.Concat(input.Select(c => encryption(c)));
}
We can even get rid of encryption
:
public override string Encrypt(string input) =>
string.Concat(input.Select(c => (char)chars[(c * k1 + k0) % 26]));
并不是说它非常易读,而是将整个方法简化为一行:
public override string Encrypt(string input) => string.Concat(input.Select(c => (char)chars[(c * k1 + k0) % 26]));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.