Solved

Can anyone give example for encapsulation in C#.Net

Posted on 2014-02-05
1
341 Views
Last Modified: 2014-02-05
I know the definition for encapsulation, can anyone give a example how it is implemented in C#.Net
0
Comment
Question by:RajG1978
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39836270
You use encapsulation to hide the elements of your class that outside entities should not be able to modify, or at least should not be able to modify directly.

Let's say you had the following class for an item to be sold:

public class SaleItem
{
    public decimal OriginalPrice { get; set; }
}

Open in new window


When you are passing around instances of this class within your application, you probably don't want other parts of the application modifying the value of the OriginalPrice after it has been initially set. You can encapusulate this value so that it is read-only after its initialization:

public class SaleItem
{
    private decimal _originalPrice;

    public SaleItem(decimal originalPrice)
    {
        this._originalPrice = originalPrice;
    }

    public decimal OriginalPrice
    {
         get { return this._originalPrice; }
    }
}

Open in new window


Now you have encapsulated the original price so that only the SaleItem class itself can modify the _originalPrice--no mechanism exists for code external to the class to modify this value as it is written above. Had you not encapsulated the _originalPrice, logic like this would have been possible:

SaleItem item = new SaleItem() { OriginalPrice = -10.00M };

Open in new window


Now, by way of encapsulation, you can incorporate error checking into your property to prevent such occurrenes:

...

    public decimal OriginalPrice
    {
        get { return this._originalPrice; }
        set
        {
            if (value <= 0) throw new ArgumentOutOfRangeException("Price cannot be less than zero!");

            this._originalPrice = value;
        }
    }
}

Open in new window

0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question