Programming Hub C,C++,C#,Asp.Net,Ado.Net,Java,  HTML,SQL.

This Blog Post Only Education Purpose For All Education Related Blogs and Articles Post Here.Download Free Software and Study Materials Mores..

Wednesday, 23 July 2014

Delegate in C#

                  Delegate in C#

Contents

*    What is delegate?
*    How to declare delegate by keyword?
*    Types of delegate?
*    Example program in use delegate?
*    Output of the example program?


   “Delegate is a reference type method which holds the reference method.”
                                                               “Delegates are use at run time”.
Declare delegate below this keyword:
Public delegate void mydelegate(String s);
@@There are two types of delegate:
1.     Single cast delegate:-“A single cast delegate contains reference to only one method at a time".
2.     Multi cast delegate:- “A multi cast delegate hold the reference of more than method and execute all the method.it wraps in the calling order”.
@@example of delegate program print to device@@@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Delegates
{
    public class Program
    {
        static FileStream fstream;
        static StreamWriter swriter;
        public delegate  void printdata(String s); //apply delegate this program
        public static void Writeconsole(String str)
        {
            Console.WriteLine("", str);
        }
        public static void WriteFile(String s) // name method
        {
            fstream = new FileStream("C:\\Users\\KRISHNA SINGH\\Desktop\\Hi3.txt", FileMode.Append, FileAccess.Write); // Apply to system location for create file u can select any other location  for print file...and  u can create .doc file & .sxlx file & .pps file and more…
            swriter = new StreamWriter(fstream);
            s = s +"";a
            swriter.WriteLine(s);
            swriter.Flush();
            swriter.Close();
            fstream.Close();
        }
       
        public static void DisplayData(printdata pmethod)
        {
            pmethod("");
        }
        public static void Main()
        {
            printdata pd = new printdata(Writeconsole);
            printdata f1 = new printdata(WriteFile);
            DisplayData(pd);
            DisplayData(f1);
            Console.ReadLine();
        }
    }
}


Then run the program…u seen blank console CMD… just like as

Then enter the button…
Seen the output of this example of the delegate program…
Go to your system location in which location u given in program that this file print or create or not…








 Look file name is Hi3.txt…
This is your output…




SO Friends these are few Blogs that will help you in "Delegate  in C-sharp" next time i will put some more interesting Blogs thank you..I Hope Helpfull u.. enjoy :).Krishna Singh

No comments:

Post a Comment

Please Comment Your Valuable Feedback....

You Might Also Like

Developing Data-Centric Windows Applications using Java

Developing Data-Centric Windows Applications using Java Introduction The Core Java Programming and JDBC course provides an introd...

Unlimited Reseller Hosting