Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Can anyone give example for encapsulation in C#.Net

Posted on 2014-02-05
1
Medium Priority
?
357 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

618 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