unsafe static void Main() { // Wrap the methods inside delegate instances and pass to the method. UppercaseDelegate uppercaseFirst = new UppercaseDelegate(uppercaseFirst = a => a.Replace(a[0], (char)(a[0] - 0x20))); Console.WriteLine(" With delegate \" UppercaseFirst \""); WriteOutput("perls", uppercaseFirst); //UppercaseDelegate uppercaseLast = new UppercaseDelegate(uppercaseLast = a => a.Replace(a[a.Length - 1], (char)(a[a.Length - 1] & 0xDF))); //https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/how-to-modify-string-contents UppercaseDelegate uppercaseLast = new UppercaseDelegate(uppercaseLast = a => { fixed (char* p = a) { p[a.Length - 1] &= (char)0xDF; } return a; }); Console.WriteLine(" nWith delegate \" UppercaseLast \""); WriteOutput("perls", uppercaseLast); UppercaseDelegate uppercase = new UppercaseDelegate(uppercase = a => a.ToUpper()); Console.WriteLine(" nWith an anonymous method which put all characters uppercase "); WriteOutput("perls", new UppercaseDelegate(uppercase)); Stopwatch stopWatch1 = new Stopwatch(); stopWatch1.Start(); for (int i = 0; i < 1000000; i++) { uppercaseLast.Invoke("perls"); } stopWatch1.Stop(); Console.WriteLine(" UppercaseLast {0} [ms]", stopWatch1.ElapsedMilliseconds); }
//static void WriteOutput(string input, UppercaseDelegate del) //{ // Console.WriteLine("Your string before: {0}", input); // Console.WriteLine("Your string after: {0}", del(input)); //} static void Main(string[] args) { string input = "adnsu"; UppercaseDelegate del = new UppercaseDelegate(UppercaseFirst); string result = del(input); Console.WriteLine(result); del = new UppercaseDelegate(UppercaseLast); result = del(input); Console.WriteLine(result); del = new UppercaseDelegate(UppercaseAll); result = del(input); Console.WriteLine(result); //WriteOutput("perls", new UppercaseDelegate(UppercaseFirst)); //WriteOutput("perls", new UppercaseDelegate(UppercaseLast)); //WriteOutput("perls", new UppercaseDelegate(UppercaseAll)); }
static void WriteOutput(string input, UppercaseDelegate del) { Console.WriteLine("Your string before: {0}", input); Console.WriteLine("Your string after: {0}", del(input)); }
static void WriteOutput(string input, UppercaseDelegate upper) { Console.WriteLine(" Your string before : {0}", input); Console.WriteLine(" Your string after : {0}", upper.Invoke(input)); }
//delegate1 public static void AllCases(string input, UppercaseDelegate del) { Console.WriteLine("Before: {0}", input); Console.WriteLine("After: {0}", del(input)); }
public void WriteOutput(string input, UppercaseDelegate dele) { Console.WriteLine($"{input} - {dele(input)}"); }
static void WriteOutput(string input, UppercaseDelegate del) { Console.WriteLine("Input String: {0}", input); Console.WriteLine("Output String: {0}", del(input)); }
private static void WriteOutput(string input, UppercaseDelegate del) { Console.WriteLine($"Your string before: {input}"); Console.WriteLine($"Your string after: {del(input)}"); }