J
J
JoraInTheSky2017-09-08 21:33:22
OOP
JoraInTheSky, 2017-09-08 21:33:22

Are properties (auto-properties) in c# the behavior or state of an object?

Actually, the question is: properties (auto-properties) in c # is the behavior or state of the object?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
wol2for, 2017-09-09
@JoraInTheSky

Let's define terminology:
I emphasize again, the object . Not a type, not a class (a class only describes the future object), but an object .

Behavior is how an object acts and reacts; behavior is expressed in terms of object state and message passing. In other words, the behavior of an object is its observable and externally verifiable
activity
. ( Gradi Butch )

And now let's figure out what's what.
An example from the same Richter chapter 9.
public sealed class Employee 
    {
    private String m_Name;   // Это состояние 
    private Int32  m_Age;    // Это состояние 
    
    public String GetName() // Это поведение  
    {
       return(m_Name);
    }
    public void SetName(String value) // Это поведение
    {
       m_Name = value;
    }

Equivalent to this example from the same chapter (Richter himself says this):
public sealed class Employee 
    {
       private String m_Name; // Это состояние 
       private Int32  m_Age; // Это состояние 
    
       public String Name // Это поведение  
       {
         get { return(m_Name); }
         set { m_Name = value; }
       }  
    }

Here it already becomes clear that properties are a mechanism for accessing private fields, which is carried out through methods. This means that Properties are behavior . Richter just criticizes properties for being like fields, but in fact they are methods (which provide access to private fields). Richter generally proposes to remove properties in order to avoid confusion (in the book he considers the problem with properties from the other side, but this is another facet of the problems associated with properties).
And, speaking of properties in interfaces, they are marked as abstract, and look something like this:
public abstract String GetName();
public abstract void SetName(String value);

They are just bare methods with no implementation. They cannot be either a state or a behavior, since only an OBJECT can have a state and a behavior.
An instance of an interface and an abstract class cannot be instantiated, so the concept of state and behavior cannot be applied to them.

S
Sanan Yuzb, 2017-09-08
@Sanan07

This is the state of the object. These are the same getters and setters, that is, getters and setters are created during compilation. This is a kind of syntactic sugar.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question