delegate simple example

The following is a simple example of declaring and using a delegate.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication10
{
    class Program
    {
        private delegate string GetAString();

        static void Main(string[] args)
        {
            int x = 40;
            
GetAString firstStringMethod = new GetAString(x.ToString);

// the above line can be written as
// GetAString firstStringMethod = x.ToString;

            Console.WriteLine("String is {0}", firstStringMethod());           
        }
    }
}

 

Tags:,

Add a Comment

Your email address will not be published. Required fields are marked *