what is the difference between constructor and properties

mohammad hoseini 0 Reputation points
2023-02-04T08:18:06.5933333+00:00

i mean in case we should declare a field as private
is it better to declare a public property or using constructor?
why?
and if NO
what is the usage of property?

thank you very much :)

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,786 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,827 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Reza Aghaei 4,946 Reputation points MVP
    2023-02-04T11:12:57.04+00:00

    Sometimes both are necessary, sometimes none of them, sometimes one of them.

    Ask these questions from yourself:

    • Does your class necessarily need to know values of those private fields at time of construction? If it's necessary to know the values of the fields at the time of construction, and without those values the object cannot have any meaningful default values for the fields and the object doesn't make sense without those values, then you need to have parameters in constructor to set values of those fields. For example, if a circle class doesn't make sense without a value for radius and there's no good default value for that, then you need to have a radios parameter value for constructor. But if you can have a default value, and the default value is acceptable, then you don't necessarily need constructor parameter.
    • Does your class necessarily need to know values of those private fields at time of construction? If it's necessary to know the values of the fields at the time of construction, and without those values the object cannot have any meaningful default values for the fields and the object doesn't make sense without those values, then you need to have parameters in constructor to set values of those fields. For example, if a circle class doesn't make sense without a value for radius and there's no good default value for that, then you need to have a radios parameter value for constructor. But if you can have a default value, and the default value is acceptable, then you don't necessarily need constructor parameter. Another example, if a your class has a dependency to another class, and without that dependency your class cannot work, then you need to accept it in constructor.
    • Do you need to modify or read those values from outside, after construction? If you need to read or modify those values from outside after the construction, then you need properties. Or if you should not let the values be modifiable, then you may need a read-only property, or if those values should not be visible outside of class, then you don't need a property. For example, after creating a circle, if it's possible to modify or read radius, then you need a property for this purpose. Another example, you may want to pass connection string to class, at time of construction, but you may not want to let other classes see the value, then you should not have a property for that.
    • Does it makes instantiation easier with parameters? There are some cases that you may want to have multiple constructors, to make it easier to construct the class. In Circle example, if zero is an acceptable radius, then you may want to have Radius property, also a constructor which accepts radius, and also a constructor without any parameter; where in this case it uses zero as default radius. Or there are some cases, that the parameters doesn't match exactly with the fields, for example imagine a Rectangle which has Location and Size, but you can accept X, Y, With and Height at the time of construction to make the construction easier.
    • Is it necessary for my framework to have a parameterless constructor? There are some cases that a framework mandates you to have a parameterless constructor; for example because of serialization/deserialization purpose or etc.
    1 person found this answer helpful.

  2. Bruce (SqlWork.com) 63,916 Reputation points
    2023-02-04T17:21:00.6833333+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.