6
Answers

Model class constructor?

Tahir Ansari

Tahir Ansari

2y
640
1

Hi,

anyone explain me for understanding

public class Action
    {
        public Action()
        {
            Permissions = new List<Permission>();
            this.CreatedOn = DateTime.Now;

        }

}

 

why we need constructor in model class

Answers (6)
2
Rijwan Ansari

Rijwan Ansari

4 66.3k 3m 2y

The main use of constructors is to initialize the private fields of the class while creating an instance of the class. When you have not created a constructor in the class, the compiler will automatically create a default constructor of the class.

public class Action // this is your call
{
    public Action() //// this is your constructor
    {
        Permissions = new List<Permission>(); // reset this variable whenever this class object is initialized
        this.CreatedOn = DateTime.Now; //// Assign date whenever this class object is initialized
    }
}
1
Rajeev Kumar

Rajeev Kumar

752 1.1k 73.9k 2y

It is mandatory to have the constructor in the class and that too should be accessible for the object i.e., it should have a proper access modifier. Say for example we have the private constructor in the class then it is of no use as it cannot be accessed by the object, so practically it is no available for the object. In such conditions it will raise an error. 

1
Jignesh Kumar

Jignesh Kumar

30 39.5k 2.9m 2y

Hello Tahir,

Please refer this link to understand why constructor is require for class and what is use of constructor,

https://www.c-sharpcorner.com/article/constructors-in-C-Sharp/

1
Tuhin Paul

Tuhin Paul

44 32.8k 309.1k 2y

Hello Tahir,

In object-oriented programming, a constructor is a special method that is called when an object is created. The constructor is responsible for initializing the object's fields or properties with default values or values provided by the caller.

In the case of your example, the Action class has a constructor that initializes the Permissions property to a new List<Permission> instance and sets the CreatedOn property to the current date and time using DateTime.Now. By providing a constructor with default values for these properties, you ensure that any instance of the Action class will have these values initialized, even if the caller does not provide explicit values.

In addition, constructors can be used to enforce constraints on the values that can be passed to an object. For example, you can define a constructor that takes parameters and validates that they meet certain requirements before initializing the object. This can help prevent errors and ensure that your object is in a valid state.

In general, constructors are a key part of object-oriented programming and are used to ensure that objects are properly initialized and in a valid state when they are created. By defining a constructor in your model class, you can ensure that any instance of the class will have default values or values that meet certain constraints, which can make your code more robust and reliable.

1
Vishal Joshi

Vishal Joshi

247 7.8k 134.5k 2y

Hello Tahir,

It's good practice to define a constructor in a model class. let's understand your code sample.

let's say the constructor is not defined and Permission is not initialized. then when trying to assign the value run time to Permission it will throw an error object reference not set to an instance.

So, if we initialize properties of the class in the default constructor to handle run time initialization errors. 

 

Please check the below article for more detail on the constructor.

https://www.c-sharpcorner.com/article/constructors-in-C-Sharp/#:~:text=It%20is%20a%20method%20in,can%20have%20multiple%20overloaded%20constructors.

Thanks

Vishal

1
Naimish Makwana

Naimish Makwana

137 13.8k 200k 2y

Hello Tahir,

It is not necessary that all the model class has constructor. 

But constructor is a special method that is automatically invoked when an object is created. It is used for initializing member variables, setting default values, or performing other initialization tasks.

In your example, when an action object is created, than Permissions List will be initialized and CreatedOn property will be set as today's date. This ensures that the Action object is initialized with default values for its properties when it is created.

Thanks

Naimish Makwana