C# Program to Implement Principles of Delegates

This is a C# Program to implement principles of delegates.

Problem Description

This C# Program Implements Principles of Delegates.

Problem Solution

Here delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.

Program/Source Code

Here is source code of the C# Program to Implement Principles of Delegates. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Implement Principles of Delegates
 */
 
using System;
class Program
{
      delegate string UppercaseDelegate(string input);
      static string UppercaseFirst(string input)
      {
        char[] buffer = input.ToCharArray();
        buffer[0] = char.ToUpper(buffer[0]);
        return new string(buffer);
      }
      static string UppercaseLast(string input)
      {
        char[] buffer = input.ToCharArray();
        buffer[buffer.Length - 1] = char.ToUpper(buffer[buffer.Length - 1]);
        return new string(buffer);
      }
      static string UppercaseAll(string input)
      {
        return input.ToUpper();
      }
      static void WriteOutput(string input, UppercaseDelegate del)
      {
        Console.WriteLine("Input String: {0}", input);
        Console.WriteLine("Output String: {0}", del(input));
      }
      static void Main()
      {
        WriteOutput("tom ", new UppercaseDelegate(UppercaseFirst));
        WriteOutput("tom", new UppercaseDelegate(UppercaseLast));
        WriteOutput("tom", new UppercaseDelegate(UppercaseAll));
        Console.ReadLine();
      }   
}
Program Explanation

This C# Program is used to Implement Principles of Delegates. Here delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, associate its instance with any method with a compatible signature and return type. Invoke the method through the delegate instance.

advertisement
advertisement
Runtime Test Cases
 
Input String: tom
Output String: Tom
Input String: tom
Output String: toM
Input String: tom
Output String: TOM

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.