?
Solved

Can anyone give example for encapsulation in C#.Net

Posted on 2014-02-05
1
Medium Priority
?
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 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