Showing posts with label .NET Framework. Show all posts
Showing posts with label .NET Framework. Show all posts

Delegates in C#


Delegates
  • It is a reference type, holds address of a method.
  • Allows to change reference to a method at runtime.
  • Referring method should have the return type and function signature same as that of the delegate. 

  •  The following are the steps for implementing Delegates
o   Declare a delegate
*  Syntax
[access modifier] delegate <return type> <delegate name> <parameter list>
*  E.g.
public delegate void MyDelegate();
*  This delegate can refer any method having no parameter with no return type.
o   Instantiate a delegate
*  MyDelegate delObj = new MyDelegate(staticMethod);
*  delObj is an object of type MyDelegate
*  delObj refers to staticMethod, by passing the name of method to the delegate constructor.
o   Invoke a delegate
*  Delegates are invoked using its object name and passing the parameters, if required, in a similar way of calling methods.
*  delObj();
  • Types of Delegate
o   Single-cast Delegate
*  Refers to only one method at a time.
o   Multicast Delegate
*  A single delegate can refer to multiple methods.
*  Contains an invocation list of multiple methods.
*  Invokes multiple methods provided all methods have same return type and same function signature.
*  When invoked, executes the referenced methods in the calling order.

DOWNLOAD SAMPLE CODE: CLICK HERE

What is .NET Framework ?

The .NET Framework is a Platform for software development.The .NET Framework consists of 
  1. CLR (Common Language Runtime)
  2. CLS (Common Language Specification)
  3. CTS (Common Type System)
  4. .NET base class library
CLR (Common Language Runtime) :
Provides many of the core services required for program execution.

CLS (Common Language Specification) :
The CLS defines a minimum set of  standards that all languages using the .NET Framework must support,

CTS (Common Type System)
The CTS ensures type compatibility between components developed in different languages.

.NET base class library :
The .NET base class library, which exposes a set of pre-developed classes to facilitate program development. 

The primary unit of a .NET application is the assembly, which includes an assembly manifest. The assembly manifest describes the assembly and one or more modules , and modules contain the source code for the application.

A .NET executable is stored as an IL file. When loaded, the assembly is checked against the security policy of local system. If it is allowed to run, the first assembly is loaded into memory and JIT compiled into native binary code, where it is stored for the remainder of the program's execution.
top