CS
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Simple strategy pattern with delegates
// https://stackoverflow.com/questions/3622160/c-sharp-passing-function-as-argument
// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/
using System;
namespace Sololearn
{
class StringEncoder
{
// delegate is a type, that references a method with specific param and return type
delegate string EncoderMethod(string s);
// this method receives the delegate as argument, and executes it
static string Encode(string s, EncoderMethod encode)
{
return encode(s);
}
// a custom method to process a string
static string Reverse(string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
static void Main(string[] args)
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run