Reputation: 5228
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?
Upvotes: 0
Views: 111
Reputation: 966
Not that it's very readable, but here is whole method simplified to one line:
public override string Encrypt(string input) => string.Concat(input.Select(c => (char)chars[(c * k1 + k0) % 26]));
Upvotes: 2
Reputation: 186668
You can try using Linq, i. e.
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]));
Upvotes: 2
Reputation: 2082
You can do something like this:
result = string.Concat(input.Select(c => encryption(c)))
Upvotes: 2