Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
the C# compiler exposes delegates as reference types, you could not instantiate the delegate by using the new operator.we cannot instantiate a delegate by using new key word.

please resolve it

thanx
Posted

Google it for "Delegate in c#" and you will get no. of good articles which will help you to understand the concept. :)
 
Share this answer
 
Actually delegates are istantiated, see, for instance: "Delegates Tutorial at MSDN"[^].
 
Share this answer
 
A delegate is not an instance - it is a variable holding a reference to a method.
As a result, you cannot instantiate it by using the new operator: that would imply that you are creating a new method, which is plainly silly!

If you think of a delegate as a "Pointer to a method" then you are there. It is a way to access a method later without needing to know it's name at that point.
C#
public string GetScreenName() { return "Screen"; }
public string GetPrinterName() { return "Printer"; }
public delegate string GetNameMethod();
public GetNameMethod GetName;
...
            if (isUsingScreen)
                {
                GetName = GetScreenName;
                }
            else
                {
                GetName = GetPrinterName;
                }
            Console.WriteLine(GetName());
 
Share this answer
 
Delegates is a type that reference Method. When it assigned by a method it behaves exactly same as that method. It can be used as parameters and can be returned a value. So it has same what are the methods have.

For more Read this article,

http://www.pathumf.blogspot.com/2011/11/delegates-in-c.html
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900