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.


At Assemblysoft we specialise in Custom Software Development tailored to your requirements. We have experience creating Booking solutions, as we did for HappyCamperVan Hire. You can read more here.

We can onboard and add value to your business rapidly. We are an experienced Full-stack development team able to provide specific technical expertise or manage your project requirements end to end. We specialise in the Microsoft cloud and .NET Solutions and Services. Our developers are Microsoft Certified. We have real-world experience developing .NET applications and Azure Services for a large array of business domains. If you would like some assistance with Azure | Azure DevOps Services | Blazor Development  or in need of custom software development, from an experienced development team in the United Kingdom, then please get in touch, we would love to add immediate value to your business.

Assemblysoft - Your Safe Pair of Hands

https://assemblysoft.com/


Comments

Popular posts from this blog

Windows Azure Storage Emulator failed to install

CodeProject Windows Azure Storage Emulator failed to install When attempting to install a new version of the Azure Storage Emulator either as a separate installation package or automatically as part of an Azure SDK update, you may run into an error message which states the storage emulator has failed to install. This can occur using the Web Platform Installer (WebPI), NuGet Package Manager or when performing the install manually. Below is the message received using the WebPI.   Storage Emulator Background  (optional reading) The windows azure storage emulator executable lives under the Microsoft SDKs directory as shown below: Configuration If we take a quick look inside the WAStorageEmulator.exe.config file we can see each of the storage services pointing to local service endpoints. <StorageEmulatorConfig>     <services>       <service name=" Blob " url="http://127.0.0.1:10000/"/>       <service

Debugging Python and Iron Python using Visual Studio

Now Python is a first class citizen since the release of Visual Studio 2017 and can be configured directly from the Installation IDE, below are a few settings worth bookmarking for your next python integration project. Debugging Python One of the first things you are going to want to do is step through your code when using Visual Studio, particularly as the language is dynamic and inspection of local and global scope soon becomes necessary. One thing to note is that if you start from a native python project, this is all wired up for you but if you are using .Net to call python modules or want to support an older python version, such as 2.7, you will soon see that breakpoints are not being hit due to symbols not being loaded.   Enable Just My Code To distinguish user code from non-user code in .net, Just My Code looks at two things: PDB (Program Database) files, and Optimization Program Database A .pdb file, otherwise known as a symbol file, maps the identifiers

Test connection to remote SQL Server Database from an Application server

While aiming to test whether a SQL connection is succeeding between an Application server and a remote Database Server, it is often not possible to install SQL Server Management Studio (SSMS) or Microsoft Command Line Utilities (MsSqlCmdLnUtils) due to the locked down nature of the targets, particularly in test and production environments. A lightweight approach that worked for me recently, makes use of components that have been a part of windows boxes for a long time, albeit different levels of database driver support as the components have evolved, the Microsoft Data Access Components (MDAC). MDAC provide a Universal Data Link, which can be configured using a common user interface for specifying connection properties as well as testing the connection.  Data Link properties dialog box At Assemblysoft we specialise in  Custom Software Development  tailored to your requirements. We have experience creating Booking solutions, as we did for HappyCamper