-3

I am new to programming, could someone please explain me difference between constructor and property in context to C#. since both used to initialized your class fields, & also which one to choose in a given situation .

someone
  • 105
  • 2
  • 7

3 Answers3

4

Besides all the technical stuff, a good rule of thumb is to use constructor parameters for mandatory things, properties for optional things.

You can ignore properties (hence optional), but you can't ignore constructor parameters (hence mandatory).

For everything else, I'd recommend reading a C# beginners book or tutorial ;-)

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
1

A property is just a class member that can be initialized when ever.

Like so:

var myClass = new MyClass();
myClass.PropertyA = "foo";
myClass.PropertyB = "bar";

A constructor is run when the class is created and can do various things. In your "scenario" it would probably be used to initialize members so that the class is in a valid state upon creation.

Like so:

var myClass = new MyClass("foo", "bar");
Snæbjørn
  • 10,322
  • 14
  • 65
  • 124
0

Constructor is a special type of method from the class to create the object itself. You should use it to initialize everything necessary to make the object work as expected.

From MSND Constructor:

When a class or struct is created, its constructor is called. Constructors have the same name as the class or struct, and they usually initialize the data members of the new object.

Properties enable a class to store, setting and expose values needed for the object. You should create to help in the behavior for the class.

From MSND Property:

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

Example:

public class Time
{
   //
   //  { get; set; } Using this, the compiler will create automatically
   //  the body to get and set.
   //
   public int Hour { get; set; } // Propertie that defines hour
   public int Minute { get; set; } // Propertie that defines minute 
   public int Second { get; set; } // Propertie that defines seconds 

   //
   // Default Constructor from the class Time, Initialize
   // each propertie with a default value
   // Default constructors doesn't have any parameter
   //
   public Time()
   {
      Hour = 0;
      Minute = 0;
      Second = 0;
   }


   //
   // Parametrized Constructor from the class Time, Initialize
   // each propertie with given values
   //
   public Time(int hour, int Minute, int second)
   {
      Hour = hour;
      Minute = minute;
      Second = second;
   }
}

Properties should be used to validate the values passed as well, for exemple:

public int Hour 
{ 
   //Return the value for hour
   get
   {
     return _hour;
   } 
   set
   {
     //Prevent the user to set the value less than 0
     if(value > 0)
        _hour = 0; 
     else
        throw new Exception("Value shoud be greater than 0");
}
private int _hour;

Hopes this help you to understand! For more information about C# take a look at Object-Oriented Programming (C# and Visual Basic).

Fals
  • 6,813
  • 4
  • 23
  • 43