Skip to main content

.Net Delegates

Delegates, Anonymous Delegates, Lambdas, 

This article (part1) forms part of a mini series which will

 and Generics

What is a Delegate?

You may be wondering why another article on delegates. If you are already comfortable using them, maybe you are done here, or maybe you have the job of mentoring other developers in your team and have found it tricky to explain in the real world, or maybe you have sat down with a team member and they still don't really get them. On my travels i frequently meet developers who failed to really grasp delegates earlier on and thus struggle with many other topics which build on a solid understanding of what delegates are. For example: Lambda Expressions, Reading and Writing queries targeted at LINQ to EntitiesEvents and Generics to name a few.

If we search MSDN the definition we find is as follows
delegate is a reference type that can be used to encapsulate a named or an anonymous method. Delegates are similar to function pointers in C++; however, delegates are type-safe and secure.

You may come from a C++ background like myself in which case function pointers were an essential part of calling libraries from third party vendors and frameworks to get hold of OS information and tooling. However if not, it may take a while and some understanding of what problems are solved through using delegates to really start using them day to day and at which point they will become second nature.

This article, Part 1, aims to form a series of articles with source code that will build on top of one another to progressively build the delegate story...

Can i ask again, What is a Delegate?

My first answer would be "You can think of a delegate as an object that holds one or more methods".
 Normally we don't just go ahead and execute an object, but rather we do the following:

  • Declare the type
  • Declare a variable of the type
  • Instantiate the variable and set properties
  • Use the variable
//Declare the type
public class Car
{
private int speed = 0;

public string Make{get;set;}
public string Model{get;set;}

public void Accelerate()
 {
  speed = 210
 }
}

//Declare a variable of the type
Car fastCar;

//Instantiate the variable and set properties
fastCar = new Car(){ Make = "Porshe", Model = "950" };

//Use the variable
fastCar.Accelerate();


A delegate is a type that has already been created by the .Net team.
For a delegate we do something similar:

  • Declare the delegate type
  • Declare a variable of the delegate type
  • Create an instance of the delegate and set it's reference 



A delegate is already a type that is part of the .Net framework class library



Multicast

Every delegate is a 'multicast delegate', which means each delegate maintains an invocation list of all the methods it needs to invoke when the delegate itself is invoked or called.


If you would like some hands on expertise for your business feel free to reach via my company assemblysoft

Comments

Popular posts from this blog

.Net TDD (Test Driven Development) by example - Part 1


Introduction In part 1 of this mini-series, we will develop a trivial business logic layer from scratch with a TDD approach with the goal of achieving the following:

Better code quality through Red, Green, RefactorDocumentation that grows as we develop and remains up to dateAutomatic regression test harness
This will primarily involve creating unit testsfirst, having them fail, making them pass and then refactoring the code to be of better quality and then re-running the tests. When using tools such as resharper to aid in refactoring code, having the tests in place right from the beginning really gives you peace of mind that you haven't broken anything. It also helps the thought processes while designing and developing an application or feature to be more targeted.

We will further develop the application in part 2 to add an MVC4 web client and continue the TDD story... 


Some BackgroundTest First or Test Driven development is a valuable software engineering practice. It c…

Azure Devops - Pull Request Merge Conflicts

Before a Git pull request can complete, any conflicts with the target branch must be resolved. Out of the box, at the time of writing this article, Azure DevOps requires this to be resolved locally. Following best practices to not allow direct commits to our release/master branches further exasperates the problem as we need to effectively clone the branch or go with a rebase approach, both of which break the natural flow of resolving the conflicts as part of the pull request.

With this extension, from the Microsoft DevLabs team via the Marketplace, you can resolve these conflicts online, as part of the pull request process, instead of being forced to break flow and resolve locally.




Online Experience After adding the extension the new conflicts tab is visible which enables conflict resolution in the familiar side by side review page as shown below:


Really nice extension, which should make resolving merge conflicts a much more straightforward part of the DevOps workflow. 

Additional - Ad…

Simple Git branching strategy for release cycles

Coming up with a branching strategy that works well can be challenging when working with multiple developers and managing release cycles. A simple approach is presented here to manage release cycles, with a small to medium sized team of developers while still being able to react to production issues and fix bugs. The primary goal being to isolate work streams without impacting development progress. BackgroundGit does not enforce any particular strategy when it comes to branching which is partly what makes it such a great and flexible repository. The problems start to arise though as you move into different stages of your development process. As an example, you have a release almost complete but don’t want to impede progress on the upcoming release cycle which is where the majority of effort is required. The Basic ApproachThe focus is around producing a release while still being able to react to hotfixes or production issues without impacting on going development of features. The branches…