4 Replies - 271 Views - Last Post: 20 February 2012 - 04:45 PM Rate Topic: -----

#1 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 362
  • Joined: 14-December 08

Delegate Object Target

Posted 19 February 2012 - 02:25 AM

Well...Now I am studying in depth of delegate.

I found that the explanation or discussion of the object target property of the delegate is rather confusing.

at MSDN, it is written that this returns the instance of the instance method on which the delegate will invoke.

the book I read C# advanced or something, the author said, it will return the object that represents the method that the delegate will invoke on.

However, the result of the code returns the NAME OF THE CLASS of the instance method maintained by the delegate.

So I would really like to know the precise and true definition/explanation of the object target property of the delegate Hope somebody could clarify this.

This post has been edited by kenryuakuma: 19 February 2012 - 02:29 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Delegate Object Target

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 916
  • View blog
  • Posts: 921
  • Joined: 30-September 10

Re: Delegate Object Target

Posted 19 February 2012 - 04:44 AM

Hi,

Both those definitions you quoted are accurate. The Target property simply holds the instance on which the method represented by the Method property will be invoked.

This is fully supported by the fact that if you try and print the value of the Target property (I assume that is what you mean, correct me if I am wrong), you get a class name.

For example:

Spoiler


If I ran that code, I would see MyConsoleApp.Program printed out to the screen. Why? Because I am passing in an object (the value of the Target property) to Console.WriteLine(), and in order to display it, ToString() has to be called on the object to get its string representation, of which is suitable for display. Seeing as we haven't overridden ToString() in our Program class, the Object class' default version gets called, of which simply returns the fully qualified type name of the object.

Hence, as we see MyConsoleApp.Program printed out in the above code, it is confirming that the underlying type of the object held in the Target property is indeed Program, and as that is the type of object Test1() is invoked on, the definitions you cited are clearly correct.
Was This Post Helpful? 1
  • +
  • -

#3 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 362
  • Joined: 14-December 08

Re: Delegate Object Target

Posted 19 February 2012 - 03:30 PM

Thanks a lot for the clarification. But something I'd like to know more about is that when you mentioned about the Object class' default version gets called, are you saying the program class or are you talking about the default super class OBJECT?
Was This Post Helpful? 0
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 916
  • View blog
  • Posts: 921
  • Joined: 30-September 10

Re: Delegate Object Target

Posted 20 February 2012 - 02:34 AM

I am talking about the super class Object. That class defines ToString() like this:

public virtual string ToString()
{
   return this.GetType().ToString();
}



As we didn't override that method in the Program class, that is the version of the method that is used when we call ToString() on a Program instance :)
Was This Post Helpful? 0
  • +
  • -

#5 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 362
  • Joined: 14-December 08

Re: Delegate Object Target

Posted 20 February 2012 - 04:45 PM

Thanks a lot for clarifying this.

It seems the definition of the MSDN seems accurate and makes sense.

But the second definition seems incorrect, which is what the author said in the book. :lol:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1