Sunday, 18 March 2018

Properties - C#

Properties are special kind of class member, In Properties we use predefined Set or Get method.They use accessors through which we can read, written or change the values of the private fields.

For example, let us Take a class named Employee, with private fields for name,age and Employee Id. We cannot access these fields from outside the class , but we can accessing these private fields Through properties.

Why We use properties
Marking the class field public & exposing is a risky, as you will not have control what gets assigned & returned.

To understand this clearly with an example lets take a student class who have ID, pass mark , name.Now in this example some problem with public field
  1. ID should not be -ve.
  2. Name can not be set to null
  3. Pass mark should be read only.
  4. If student name is missing No Name should be return.
To remove this problem We use Get and set method.
// A simple example
public class student
{
public int ID;
public int passmark;
public string name;
public class programme
{
    public static void main()
    {
       student s1 = new student();
       s1.ID = -101; // here ID can't be -ve
       s1.Name = null ; // here Name can't be null
    }
}

Now we take an example of get and set method

public class student
{
    private int _ID;
    private int _passmark;
    private string_name ;
    // for id property
   public void SetID(int ID)
   {
       if(ID<=0)
       {
         throw new exception("student ID should be greater then 0");
       }
       this._ID = ID;
    }
    public int getID()
    {
       return_ID;
     }
   }
   public class programme
   {
       public static void main()
       {
         student s1 = new student ();
         s1.SetID(101);
      }
      // Like this we also can use for Name property
      public void SetName(string Name)
      {
        if(string.IsNullOrEmpty(Name))
        {
          throw new exeception("name can not be null");
        }
        this._Name = Name;
     }
     public string GetName()
     {
        ifstring.IsNullOrEmpty(This.Name))
        {
          return "No Name";
       }
       else
      {
        return this._name;
      }
      // Like this we also can use for Passmark property
      public int Getpassmark()
      {
        return this._passmark;
      }
}

1 comment:

  1. Hello Suraj,


    Gratitude for putting up this prolific article! You truly make everything a cake walk. Genuinely good stuff, saving time and energy.

    Is it possible to implement a server side feature that will export PivotGrid view to Excel/PDF/Word files based on the same datasource that angular control (ej-pivotgrid) is?
    I mean this structure:
    $scope.datasource = {
    data: [],
    values: [],
    rows: [],
    columns: []
    };

    If yes can you direct me in the right way?

    But great job man, do keep posted with the new updates.

    Thanks & Regards,
    Krishna kumar

    ReplyDelete

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (49) CD (1) CI (2) CloudComputing (2) Coding (8) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (19) Entity Framework (4) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives