Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace delegateexample
{
    delegate int somedelegate(int i);
    class someclass
    {
        public static int somemethod(int i)
        {
            return i * 2;
        }
        public static int somemethod2(int i)
        {
            return i / 2;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            somedelegate process = new somedelegate(someclass.somemethod());
            process(4);

        }
    }
}
I have some code.If somemethod is stated in class Program i can write name of this method like this
C#
somedelegate process = new somedelegate(somemethod);
But how to write method in delegate,if this method situated in other class.
Posted

If I understand your question, you've almost got it correct in your code above:
C#
class Program
{
    static void Main(string[] args)
    {
        somedelegate process = new somedelegate(someclass.somemethod);  // NOTE: no parens ()
        process(4);
    }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Jan-15 19:38pm    
5ed, but there is a lot more to it. First of all, OP needs to understand using instance methods, too.
Please see Solution 2.
—SA
Solution 1 is only good for static methods. Yes, you asked about static methods, but this is not the only possibility. But this is also possible with the class instance. Compare:
C#
delegate int SomeDelegate(int i);

class Implementation {
    internal int SomeMethod(int i) { /* ... */ return i; }
    internal static int SomeOtherMethod(int i) { /* ... */ return i; }
}

class ImplementationUsage {
    internal void Demo() {
        SomeDelegate theDelegateInstance =  Implementation.SomeOtherMethod;
        theDelegateInstance(3);
        Implementation implementation = new Implementation();
        theDelegateInstance = implementation.SomeMethod;
        theDelegateInstance(4);
    }
}

Pay attention that I completely removed new SomeDelegate(/* ... */); this is a totally redundant expression, in this case (and original MSDN documentation is probably not clear about it).
Also, you don't have to declare delegate int SomeDelegate(int i); instead, you can use already available declaration based on the set of System.Func generic declarations: System.Func<int, int>.

The delegate instance class (not to be confused with delegate type) is such a structure which can carry two references: to the method to be called, and to the instance of the type implicitly passed to this method at the moment of the call. In fact, this is much more advanced type, which is pretty hard to explain in all detail. For some, please see the corresponding section of my article, "4.1 On the Nature of Delegate Instance": Dynamic Method Dispatcher[^].

It's worth mentioning that another, very useful way of implementing delegates is using anonymous methods. You have to know them well due to multiple benefits of them, especially for event handling, but also many other cases. (I'll gladly answer if you have any questions on them.)

—SA
 
Share this answer
 
v4
Comments
Matt T Heffron 6-Jan-15 19:46pm    
+5 back for completeness...
Sergey Alexandrovich Kryukov 6-Jan-15 19:57pm    
Thank you, Matt.
—SA

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