0

I am writing a program using a namespace that I did not write. Inside this namespace there is a class called Message. Now this is where I am getting confused. I am able to compile code similar to what is below:

void Func(Message message)
{
    BasePlayer player = message.Player();
}

The reason I am confused is that the method Player does not exist in the Message class and Message does not inherit any other class. Could someone explain how this could be happening? I'd be happy to share more info on request.

jeaboswell
  • 15
  • 3
  • 4
    there could be an extension for `Message` called `Player` that takes zero arguments. – Jonesopolis Apr 06 '17 at 20:22
  • 4
    Right click on Player and select Go to Definition. It takes you to the method and you can see for yourself what it is. You're probably looking at a different class called Message, or an extension method (ninja edit not at all inspired by @Jonesopolis comment) –  Apr 06 '17 at 20:22
  • 2
    Take a look at the intellinsense when you're writing down the Player method and see if it's an extension method. – dcg Apr 06 '17 at 20:22
  • 2
    `The reason I am confused is that the method Player does not exist in the Message class and Message does not inherit any other class` I promise you it does, or this would not compile - it is perhaps not in the place you are looking. As others have mentioned, extension methods let you extend a class, or if it is a partial class, it may be defined in multiple files. F12 on `Player()` will take you to the definition. – 3Dave Apr 06 '17 at 20:24
  • @DavidLively extension methods do not define partial classes – Jonesopolis Apr 06 '17 at 20:25
  • @Jonesopolis I mis-typed. – 3Dave Apr 06 '17 at 20:26

1 Answers1

3

the class may not "have" that "declared" but it may be provided by another class as an extension

you can look as this question and in the code of the accpeted response you can see that you can provide methods for other classes. even if they are sealed..

mayve that is what is happening on your case.

Community
  • 1
  • 1
CaldasGSM
  • 3,032
  • 16
  • 26