Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C#

How to use delegates in C# - step 1

Rate me:
Please Sign up or sign in to vote.
4.82/5 (52 votes)
17 Feb 20063 min read 156.8K   2.3K   46  
An article to demonstrate the use of delegates.
using System;

namespace TestDelegate
{
    class Program
    {
        delegate void Sorter(ref int[] num); 

        static void BinarySort(ref int[] numbers)
        {
            //Some Codes to sort the array with Binary algoritm
            //Finally, The array will be something like this :

            numbers = new int[] { 0,1,2,3,4,5,6,7,8,9,10};
        }

        static void QuickSort(ref int[] numbers)
        {
            //Some Codes to sort the array with Quick algoritm
            //Finally, The array will be something like this :

            numbers = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        }

        static void TreeSort(ref int[] numbers)
        {
            //Some Codes to sort the array with Tree algoritm
            //Finally, The array will be something like this :

            numbers = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        }

        static bool SortAndSearch(ref int[] numbers, Sorter srt,int member)
        {
            srt(ref numbers);

            foreach (int i in numbers)
                if (i == member)
                    return true;

            return false;
        }

        static void Main(string[] args)
        {
            int[] numbers = new int[] { 5, 9, 10, 3, 7, 2, 6, 1, 4, 8, 0 };


            Console.WriteLine("Please Enter a method for Sorting\n (Binary/Quick/Tree)?");


            //Consider Binary Search as default

            Sorter sortIt = new Sorter(BinarySort);;
            
            switch (Console.ReadLine().ToUpper())
            {
                case "QUICK":
                    sortIt = new Sorter(QuickSort);
                    break;
                case "TREE":
                    sortIt = new Sorter(TreeSort);
                    break;
            }

            Console.WriteLine("Please Enter a Number to search");

            if (SortAndSearch(ref numbers, sortIt, Convert.ToInt32(Console.ReadLine())))
                Console.WriteLine("founded");
            else
                Console.WriteLine("Not Founde");

            Console.ReadLine();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
Alireza Shirazi
C#, ASP.Net,AJAX, Elearning

Comments and Discussions