?

Log in

Previous Entry | Next Entry

What are Accessors and mutators in C#?

Look at the following example,

public class Customer
{
     publicint Age;
     .
     .
     .
 }


Now in the client code we will do something like this.

Customer c=new Customer();
c.Age=55;


But do you realize one thing even we can do something like this,

c.Age=563;
OR
c.Age=-65;


This is not a valid statement logically but it’s valid syntactically. Our program will just execute perfectly fine.

One of the pillar of Object oriented principle called Encapsulation let us protect our data by creating a wrapper around our data, logic etc. by means of functions, classes etc. In short Encapsulation also let us protects our data.

Usually we will do something like this.

public class Customer
{
       privateint Age; // data is private
       .
       .
       .
       publicintGetAge() // Data is exposed to outside world via public method
       {
               Return Age;
       }
       publicvoid SetAge(intpAge) // Updating of data is only possible via public method
       {
             If(pAge>0 &&pAge<100)
             {
                    Age=pAge;
             }
             else
             {
                   throw new Exception(“Invalid Age”);
             }
        }
 }


Now our data is protected.

Client code will look like this.

Customer c=new Customer();
c.SetAge(55);
Console.WriteLine(c.GetAge());

c.SetAge(965); // It will throw exception


In the above code snippet GetAge function which usually return the data is called Accessor method and SetAge function which updates data is called Mutator method.

Good news?

Now we are in C# things become much simpler. We can make use of properties instead of creating two functions.

public class Customer
{
       privateintpAge; .
       .
       .
       publicintAge()
       {
               get
               {
                    //Accessor
                    returnpAge;
               }
               set
               {
                   //Mutator
                   If(value>0 && value<100)
                   {
                           pAge=value;
                   }
                   else
                   {
                           throw new Exception(“Invalid Age”);
                   }
              }
        }
 }


Client code will look like this,

Customer c=new Customer();
c.Age=55;
Console.WriteLine(c.Age);

c.Age = 965; // It will throw exception


For technical trainings on various topics like WCF(Windows Communication Foundation), MVC, Business Intelligence, Design Patterns, WPF, TFS and Basic fundamentals feel free to contact SukeshMarla@Gmail.com or visit www.sukesh-marla.com

For more stuff like this, click here. Subscribe to article updates or follow at twitter @SukeshMarla

Click and see here for more .NET training.

Copy right www.questpond.com

Buy 600+ Real Time Interview Videos

Call +91-22-66752917 or mail us on questpond@questpond.com for more details on C#, .NET, ASP.NET, SQL Server interview question videos.

SQL Server interview question videos

Latest Month

January 2017
S M T W T F S
1234567
891011121314
15161718192021
22232425262728
293031    

Tags

Powered by LiveJournal.com