How can I define a method in property?

Shervan360 1,481 Reputation points
2021-10-06T18:53:01.207+00:00

Hello,

I'd like to define a method into a property in my Human class like C#: DateTime.Now.ToLongDateString();

For example:

Human.Age.toDays()

Human is class and Age is the property, return an int and toDays a method, return DateTime.

Thanks

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
323 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,279 questions
{count} votes

Accepted answer
  1. Michael Taylor 48,581 Reputation points
    2021-10-06T20:43:03.127+00:00

    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);  
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful