.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Delegates in C#

Posted By:      Posted Date: April 10, 2011    Points: 0   Category :Windows Application

Could any one tell me how "Delegate" works internally in C#?

We give function reference to Delegate variable and just call method using delegate. But I want to know, how it works internally?



View Complete Post

More Related Resource Links

Delegates and events in c#.net

People often find it difficult to see the difference between events and delegates. C# doesn't help matters by allowing you to declare field-like events which are automatically backed by a delegate variable of the same name. This article aims to clarify the matter for you. Another source of confusion is the overloading of the term "delegate". Sometimes it is used to mean a delegate type, and at other times it can be used to mean an instance of a delegate type. I'll use "delegate type" and "delegate instance" to distinguish between them, and "delegate" when talking about the whole topic in a general sense.

Delegates in .NET: A Primer on Creating Type-Safe References to Methods in Visual Basic .NET


Delegates, new in Visual Basic .NET, are type-safe, object-oriented references to methods. By using delegates, you make your methods accessible to others and therefore more extensible. This article provides an introduction to delegates, their benefits, and how they are used.Here the author shows you how to declare, create, and use delegates to invoke instance and shared methods at run time, and how to implement delegates as properties. He then goes on to provide examples of how delegates are used for callback functionality and explains the relationship between delegates and events. Finally, the article provides a glimpse at how delegates are implemented in intermediate language code by the Visual Basic .NET compiler.

Jesse Liberty

MSDN Magazine February 2003

Basic Instincts: Implementing Callback Notifications Using Delegates


Delegates are a critical aspect of the Microsoft® . NET Framework and are required learning for many programmers. They will take some time to master, but once you learn how to program with delegates, you'll be thrilled with the possibilities they offer.

Ted Pattison

MSDN Magazine December 2002

.NET Delegates: Making Asynchronous Method Calls in the .NET Environment


One of the many great features of the .NET Framework is that it has asynchronous infrastructure built in. In .NET you can call any method asynchronously by defining a delegate for the method and calling the delegate's asynchronous methods. This is beneficial to your application because when a synchronous call is made, the calling thread is blocked until the method completes whereas an asynchronous call is made on a different thread, and this allows the original thread to continue its work while the asynchronous call is in progress.This article explains delegates in .NET and how to use them to perform asynchronous calls, eliminating age-old threading problems.

Richard Grimes

MSDN Magazine August 2001

Why do we say delegates are type safe as compared pointers???


Two questions:

  •  Why do we say delegates are type safe as compared pointers ?
  • Can we define a pointer in C# ?

Question about delegates


Consider a simple method of Thread class.


One of the overloads of Start method expects a parameter of type ParameterisedThreadStart which is a delegate. It is through this delegate we tell the thread which method to execute.

My question is why cant we pass the method name directly as a parameter to Thread.Start ? Why do we need to use a delegate to ask the thread which method to execute.

I hope my question makes sense.


Delegates - Is it correct?



I created a class where the main task is get data from the DB and mapping it to some object. The problem is the same class needs to map different datareader to different object. So, what I tried to do is to get out the mapping method using delegates.

Here is part of my code. See the important rows in bold.

public class GetDetails<T>
        public delegate void DelegateMapping(T position, IDataReader reader);
        public DelegateMapping mappingMethod;

        public T Get(T instance)
            //Get IDs and Add to list
            _db.ExecuteReader(storedProcedure.ToString(), CommandType.StoredProcedure, reader =>
                while ( reader.Read() )
                    mappingMethod(instance, reader);

Delegates and Lambda Expressions


What, exactly, relationship between Delegates and Lambda Expressions? More specifically, why is using a Lambda Expression in place of a Delegate allowed?

delegates and events with interface


This is working fine and dandy but I am already starting to incur some issues with readability. I am wondering if there is a documented schema for implementing events and possibly delegates in an interface? Here is what I have so far...


namespace NSMR.Rule { public interface IRule { int pk_rule_id { get; set; } string rule_name { get; set; } DateTime date_created { get; set; } DateTime date_updated { get; set; } string created_by { get; set; } string updated_by { get; set; } string description { get; set; } int pk_facility_id { get; set; } string generated_sql { get; set; } event EventHandler OnRuleCreated; } }






can events exist without delegates

Can events exist without delegates ! either in C# or VB .NET
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend