Solved

Can anyone give example for encapsulation in C#.Net

Posted on 2014-02-05
1
335 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 74

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now