Models In Dot net core

  • by

WeatherForecast.cs

When we create the application from the console, we get a model file called WeatherForecast.cs. This contains the implementation details of the WeatherForecast class. A class contains the properties and functions that represent a particular domain model.

In the WeatherForecast class we have a set of properties namely defined in the following way.

public class WeatherForecast

   {

       public DateTime Date { get; set; }

       public int TemperatureC { get; set; }

       public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

       public string Summary { get; set; }

   }

 These properties are defined using an access modifier, the type of the property and the getter and setter function that are automatically provided by the framework.

For example the type of Date property is DateTime and one can make use of all the inbuilt functionalities provided by the DateTime class. The access modifiers are Private, Public and Protected.

  1. Private access modifier states that the property/method can only be accessed from within the particular class file in which it is defined
  2. Public access modifier states that the property/method can be accessed from anywhere in the package
  3. Protected access modifier states that the property/method can be accessed from the class in which it is defined and the classes that inherit the same class.

Apart from the access modifiers, as mentioned before we have the getters and the setters. These methods are used to get and set the values of the property from outside this location.

Leave a Reply

Your email address will not be published. Required fields are marked *