Advertisement
ivandrofly

Using Delegate

Mar 4th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 KB | None | 0 0
  1. using System;
  2.  
  3. internal class Program
  4. {
  5.     private delegate string UppercaseDelegate(string input);
  6.  
  7.     private static void Main()
  8.     {
  9.         // Wrap the methods inside delegate instances and pass to the method.
  10.         WriteOutput("ivandro", new UppercaseDelegate(UppercaseFirst));
  11.         WriteOutput("ismael", new UppercaseDelegate(UppercaseLast));
  12.         WriteOutput("ivandrofly", new UppercaseDelegate(UppercaseAll));
  13.         WriteOutput("playstation", new UppercaseDelegate(UppercaseFirstLast));
  14.     }
  15.  
  16.     private static string UppercaseAll(string input)
  17.     {
  18.         return input.ToUpper();
  19.     }
  20.  
  21.     private static string UppercaseFirst(string input)
  22.     {
  23.         char[] buffer = input.ToCharArray();
  24.         buffer[0] = char.ToUpper(buffer[0]);
  25.         return new string(buffer);
  26.     }
  27.  
  28.     private static string UppercaseFirstLast(string input)
  29.     {
  30.         char[] buffer = input.ToCharArray();
  31.         buffer[buffer.Length - 1] = char.ToUpper(buffer[buffer.Length - 1]);
  32.         buffer[0] = char.ToUpper(buffer[0]);
  33.         return new string(buffer);
  34.     }
  35.  
  36.     private static string UppercaseLast(string input)
  37.     {
  38.         char[] buffer = input.ToCharArray();
  39.         buffer[buffer.Length - 1] = char.ToUpper(buffer[buffer.Length - 1]);
  40.         return new string(buffer);
  41.     }
  42.     private static void WriteOutput(string input, UppercaseDelegate del)
  43.     {
  44.         Console.WriteLine("Your string before: {0}", input);
  45.         Console.WriteLine("Your string after: {0}", del(input));
  46.     }
  47. }
  48. // http://www.dotnetperls.com/delegate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement