Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> C# >> Delegate in C#.Net
Delegate in C#.Net
Delegate in C#.Net


by AVADHESH PATEL on 7/9/2012 7:46:52 PM

Views: 2727       Comments: 0

Delegate in C#.Net

When we make an object of a class then we can access all function of a class and Base class functions too. Here we can see that a user can simple make an object of a class and access all information of class. But in real word this is drawback of an object. 

For example we are an account holder of a bank, and we have permission to access all information, like other account holder’s details, bank deposited details and etc. think what happens if bank provide all accessibly to account user…….

 Through delegate we provide limited accessibility to user.

Delegate in technical Word …

Ø  Delegate is a keyword that behaves runtime as a class.

Ø  Class’s object has reference of a class, but delegate’s object has reference of a function.

Ø  Through delegate we can break accessibility of all function.

Ø  Through delegate we can save time.

Ø  Through delegate we can achieve dynamic method invocation.

Ø  Through delegate we called planed function.

Ø  It absolution support type softy.

Ø  Delegate is similar function pointer in c.

Ø  Delegate’s object data type not fixed.

Ø  Through delegate object we can call any function from any class but both data type and signature must be same.

Ø  Using delegate we make dynamic object.

Ø  Through delegate we achieve pure encapsulation.

Ø  Simple class provides us synchronous call back. That means a class function can’t execute parallel. In other words, till one function execution not completed then we can’t call another function but delegate achieve it Asynchronous call back.

Ø  Delegate class is independent to any class means we can declare the delegate either inside the class or outside the class.

Example

delegate int ptr();

 

    class A

    {

        public int show()

        {

            return 12;

        }

    }

 

    class B

    {

        public int display()

        {

            return 15;

        }

    }

 

 

    class Program

    {

        static void Main(string[] args)

        {

            A a = new A();

            ptr k = new ptr(a.show);

            Console.WriteLine(k());

            Console.ReadLine();

        }

    }

 

Output

22

 

    delegate int ptr();

 

    class A

    {

        public int show()

        {

            return 12;

        }

    }

 

    class B

    {

        public int display()

        {

            return 15;

        }

    }

 

 

    class Program

    {

        static void Main(string[] args)

        {

            //A a = new A();

            //ptr k = new ptr(a.show);

            //Console.WriteLine(k());

 

            B b = new B();

            ptr k = new ptr(b.display);

            Console.WriteLine(k());

            Console.ReadLine();

        }

    }

 

Outout

15

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by AVADHESH PATELRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 5080
Advertisement
MindStick DataConver
Advertise with Us
  
Copyright © 2009 - 2014MindStick. All Rights Reserved.