I want to know the difference between using Delegate Methods and using General Methods[without Delegates].
With Delegate :
delegate void DelMethod(string str);
static void Method(string str)
{
Debug.WriteLine(str);
}
Usage :
DelMethod dm = new DelMethod(Method);
dm(string);
And Without Delegate :
static void Method(string str)
{
Debug.WriteLine(str);
}
Usage :
Method(string)
What are the differences of these two??
The method without delegate is smaller and easy. But I find coders using delegated Methods frequently.
What is the reason behind this??
A delegate method is a method that the delegate object is expected to implement. Some delegate methods are required, while some are not. In IOS, most delegates are expected to conform to an Objective-C protocol; the protocol declaration will tell you which methods are optional and which are required.
Delegates allow methods to be passed as parameters. Delegates can be used to define callback methods. Delegates can be chained together; for example, multiple methods can be called on a single event. Methods don't have to match the delegate type exactly.
A data source is almost identical to a delegate. The difference is in the relationship with the delegating object. Instead of being delegated control of the user interface, a data source is delegated control of data.
Imagine a scenario in which you have a function that searches a Customer. Initially you just want to search by name so you write something like:
public Customer Find(string name)
{
foreach (var customer in database.Customers)
if (customer.Name == name)
return customer;
}
Then your specifications change and you have to implement new ways of searching (let's say "search-by-address"). No problem, we refactor our old code to:
public Customer FindByName(string name)
{
foreach (var customer in database.Customers)
if (customer.Name == name)
return customer;
}
public Customer FindByAddress(string address)
{
foreach (var customer in database.Customers)
if (customer.Address == address)
return customer;
}
They look very similar, don't they?
Now your boss tell again you to add another search functionality, the user may want to find customers by telephone number. It is getting boring, isn't it?
Fortunately developers have found a way to make other developers' life easier inventing delegates. Using them you could create one bigger, general function that helps you to avoid rewriting the same pieces of code again and again.
public Customer Find(Predicate<Customer> p)
{
foreach (var customer in database.Customers)
if (p(customer))
return customer;
}
Now you do not have to create a specialized function each time you need a new way of searching customers, so you can write:
var byName = Find(a => a.Name == "lorem");
var byAddress = Find(a => a.Address == "ipsum");
var byTelephone = Find(a => a.Telephone == "dolor");
Delegates are also useful to manage events and are used intensively also in LINQ. Just google "delegates c#" and you will discover a huge new world! :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With