0

C# class data members(fields or properties) can either be private or public or protected.

What if I want a private field for member methods use only and not to expose to the outside world?

I can continue to use a private field, without breaking encapsulation or anything right?

What I am not understanding is the two concepts: the data that we may need to expose to the outside world vs the data we may not need to do so (in the periphery of a class)..

What are these two types of data while talking about building a class?

In the below example, the private field 'name' is private to the class but is still gettable/settable to/by external world. Is the abstraction here then to 'not directly expose like 'here you go- have at it' but to add an indirect mechanism of access or update?? Is that the encapsulation we are talking about here when we talk about public fields vs public properties?

class Employee2
{
    private string name = "Harry Potter";
    private double salary = 100.0;

    public string GetName()
    {
        return name;
    }

    public void SetName(string title, string fullName)
    {
       this.name = title + fullName;        
    }

    public double Salary
    {
        get { return salary; }
    }
}

class PrivateTest
{
    static void Main()
    {
        Employee2 e = new Employee2();

        // The data members are inaccessible (private), so 
        // they can't be accessed like this: 
        //    string n = e.name; 
        //    double s = e.salary; 

        // 'name' is indirectly accessed via method: 
        string n = e.GetName();

        // 'salary' is indirectly accessed via property 
        double s = e.Salary;
    }
}
Athapali
  • 1,091
  • 4
  • 25
  • 48
  • 3
    Why are you using methods rather than a property for `Name`, and why does your setter not set anything? It's a very odd sample. – Jon Skeet Apr 30 '13 at 15:02
  • Thanks, Jon for the question. I actually grabbed the example from here: http://msdn.microsoft.com/en-us/library/st6sy9xe.aspx – Athapali Apr 30 '13 at 15:04
  • I suggest you read encapsulation on Wikipedia [link](http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)) – Tombala Apr 30 '13 at 15:05
  • 1
    @SarikaThapaliya: You then added a `SetName` method without any parameters, which is very odd. – Jon Skeet Apr 30 '13 at 15:06
  • 1
    If you rename `name`, how many external classes must be touched: none. If you rename the method `SetName`, how many: all that use that public method. – Davin Tryon Apr 30 '13 at 15:06
  • 1
    I think you'll find all your answers here: http://stackoverflow.com/questions/1180860/public-fields-versus-automatic-properties?rq=1 – David Martin Apr 30 '13 at 15:09

3 Answers3

3

By using the protected keyword, you can limit a property or method to be accessible from within the class in which it is declared, and from within any class derived from the class that declared this member. (MSDN)

class Employee2
{
    private string name = "Harry Potter";
    private double salary = 100.0;

    protected string GetName()
    {
        return name;
    }

    protected string SetName(string newName)
    {
        this.name = newName;
    }

    protected double Salary
    {
        get { return salary; }

    }
}
Jean-François Beaulieu
  • 4,305
  • 22
  • 74
  • 107
0

If you are asking what's the point of having public get/set accessors when you could just make the actual field public, it's because then you have the freedom to change the class' private implementation of that field later, without changing client code. For example, you could add a check if the setter is getting valid input, or even change the datatype of the private field entirely, but translate it correctly in the setter.

edit:

SetName() as mentioned in commentaries, is an odd addition. It should actually change the name (and take a parameter), not return a string.

David S.
  • 5,965
  • 2
  • 40
  • 77
0

encapsulation means that the internal representation of an object is generally hidden from view outside of the object's definition. Typically, only the object's own methods can directly inspect or manipulate its fields

       private decimal accountBalance = 500.00m;

       public decimal CheckBalance() 
          {
            return accountBalance;
          }

and where get and set are useful for triggering events on change of values of object members

Shafqat Masood
  • 2,532
  • 1
  • 17
  • 23