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

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (24) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (9) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (12) Web Api (9) Web Service (1) XMl (1)