I believe you are confused on your terminology. Human.Age.toDays()
is not a method defined on a property. This is simply an expression. Let's break it apart.
Human
is some identifier you have in your code. It might be a local variable, parameter, or property on the class you are currently in. Doesn't matter here but what does matter is what that type is. Let's assume it is of type HumanBeing
. In order to do this Human.Age
there must be a property defined on the HumanBeing
type called Age
. Perhaps like this.
public class HumanBeing
{
public int Age { get; set; }
}
Now, given a variable of type HumanBeing
you can access the Age
property using the member access operator (.).
HumanBeing human = new HumanBeing();
human.Age = 10;
HumanBeing human2 = new HumanBeing();
human2.Age = human.Age;
The return value of the Human.Age
property is an integer. From that expression you can now access the members of the int
type such as ToString
.
string result = human.Age.ToString(); //Call the ToString method on the Age property of the variable human
The ToString
method has nothing to do with HumanBeing
but rather it is a method of the int
type. You can chain member calls together indefinitely until you eventually call a method that has no return value.
So, in your example code look at the type of the property Age
on HumanBeing
. Then go to that types definition and you should find the toDays
method. Alternatively right click the toDays
call and select Go to Implementation/Definition
if you are in Visual Studio.
In more advanced cases you can "extend" a type with new methods by defining an extension method. This is not something you should be doing until you have a good grasp of types and members. However just in case the toDays
method is an extension method, when you go to definition on it you might end up in a static class rather than type of the property. In this case you are seeing an extension method. An extension method allows us to extend a type with new methods when it is not practical to modify the type itself (such as ints). In this case we define a static method on a static class and accept the value as the first argument with the keyword this
. The compiler then allows the static method to be called as though it is a method on the type when in fact it isn't. This is really for discoverability and readability. Under the hood the code is converted to a call to the static method of the static class. Thus your toDays
method might look like this.
public static class MyIntExtensions
{
public static int toDays ( this int source )
{
return source;
}
}
//Call as either an extension method
int age = 10;
age.toDays();
//or as a regular static method
MyIntExtensions.toDays(age);