C# | DELEGATES
We all learned how to implement
REFERENCE TYPEs using languages constructs like
- · CLASSES
- · REFERENCES
These references types allow
programmers to create instances of objects and use them in special ways to
accomplish development procedure aims.
Classes allows us to create object
that contained members with attribute or the functionality. While on other hand
interface allows us to declare a set of attributes and behavior that all
objects implementing them can expose them publicly or privately.
Now am introducing a new reference type
DELEGATE.
A delegate is a C# language element that allows programmers to reference a method for their development work. Generally C or C++ programmers are much more familiar with the concept of delegate in comparison to other.
C# DELEGATE | Features
- Maximum Flexibility
- Several built in functionalities at runtime
- Manipulation of variables
C# DELEGATE | Description
Delegates provide flexibility and
scalability, so that is can be used according to the requirement or goal of your
development procedure. There is less boundation limits of delegates. Interfaces
also can be used in place of delegates but due to some extra functionality
programmers do prefer delegate in place of other reference methods.
C# DELEGATE | Declaration
Declaration of delegates is
somewhat similar like methods. Except that delegate have an extra feature of
delegate modifier. Delegate modifier are terminated through semi colon(;) and
requires no implementation
Ex:
public delegate int Comparer(object obj1, object obj2)
public delegate int Comparer(object obj1, object obj2)
C# DELEGATE | Delegate Handler
Delegate handler methods works as
follows:
Public static int CompareFirstNames(object
name1, object name2);
{
……
}
Reference Example:
using System;
public delegate int Comparer(object obj1, object obj2);
public class Name
{
public string FirstName =
null;
public string LastName =
null;
public Name(string first,
string last)
{
FirstName = first;
LastName = last;
}
public static int
CompareFirstNames(object name1, object name2)
{
string n1 =
((Name)name1).FirstName;
string n2 =
((Name)name2).FirstName;
if (String.Compare(n1,
n2) > 0)
{
return 1;
}
else if
(String.Compare(n1, n2) < 0)
{
return -1;
}
else
{
return 0;
}
}
public override string
ToString()
{
return FirstName + "
" + LastName;
}
}
class SimpleDelegate
{
Name[] names = new Name[6];
public SimpleDelegate()
{
names[0] = new
Name("abhishek", "jaiswal");
names[1] = new Name("ashish",
"verma");
names[2] = new
Name("gopi", "chand");
names[3] = new
Name("hari", "prakash");
names[4] = new
Name("prashant", "kumar");
names[5] = new Name("pratiyush",
"anand");
}
static void Main(string[]
args)
{
SimpleDelegate sd = new
SimpleDelegate();
Comparer cmp = new
Comparer(Name.CompareFirstNames);
Console.WriteLine("\nBefore Sorting: \n");
sd.PrintNames();
sd.Sort(cmp);
Console.WriteLine("\nAfter Sorting:
\n");
sd.PrintNames();
}
public void Sort(Comparer
compare)
{
object temp;
for (int i=0; i <
names.Length; i++)
{
for (int j=i; j <
names.Length; j++)
{
if (
compare(names[i], names[j]) > 0 )
{
temp =
names[i];
names[i] =
names[j];
names[j] =
(Name)temp;
}
}
}
}
public void PrintNames()
{
Console.WriteLine("Names: \n");
foreach (Name name in
names)
{
Console.WriteLine(name.ToString());
}
}
}
No comments:
Post a Comment