Solved

Can anyone give example for encapsulation in C#.Net

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 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