1

Will a private property same as a public field? Here is the sample code I wrote to understand this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GetSet
{
    class Program
    {
        private int Age_private { get; set; }
        public int Age_public;
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Age_public = 5;
            p.Age_private = 10;  // calling set
            int m = p.Age_public;
            int n = p.Age_private; // calling get
            Console.WriteLine(m);
            Console.WriteLine(n);
            Console.ReadLine();
        }
    }
}

Am I doing this correctly?

Also, does it follow that a public field will never require a getter and setter?

Reference: When are Getters and Setters Justified

  • 1
    If you're trying to understand public/private distinction, you really ought to write your example with more than one class. – Brandin Jun 17 '15 at 12:06
6

Am I doing this correctly?

No. The idea of properties is that the property itself is public and provides access to private fields. Your example however does the opposit by declaring the property private while the field is exposed public.

What it should look like:

class Program
{
    private int Age_private;    //private field, not visible outside of class
    public int Age_public
    {      //public property to manage access to age
        get { return Age_private; }
        set
        {
            if (value == Age_private + 1)
            {
                Age_private = Age_private + 1;
            }
        }
    }
    public Program()
    {
        Age_private = 0;
    }

    static void Main(string[] args)
    {
        Program p = new Program();
        p.Age_public = 1;     //increment age using setter
        int m = p.Age_public;        //get age from getter
        Console.WriteLine(m);
        p.Age_public = 2;     //increment age again using setter
        m = p.Age_public;
        Console.WriteLine(m);
        p.Age_public = 5;     //use setter with invalid value
        m = p.Age_public;
        Console.WriteLine(m);    //age is unchanged because setter didn't change the private field
        p.Age_public = -50;     //use setter with invalid value 
        m = p.Age_public;
        Console.WriteLine(m);    //age is still unchanged because setter didn't change the private field
        Console.ReadLine();
    }
}

DO NOT: expose fields outside of the class

DO: provide appropriate methods/properties to access fields where needed

note that the setter of the property only allows to increase the age by one as suggested in the answer you referenced. A property without logic would again be subject to the answer you referenced because it exposes the field without any control those breaking encapsulation again.

2

Yes, this solves your purpose but the story is different.

Fields with getters and setters are known as properties. If you don't set getters and setters explicitly then it's known as member variables or simply variables.

So say, if you code this...

public int a { get; set; }
private int b { get; set; }

These are known as properties.

Or, these are known as member variables.

public int a;
private int b;

Then also, it does the same thing and you can do the assignment and retrieval as like below in both the cases (provided you are not breaking access modifiers logic).

a = 1;
b = 2;

int c = a;
int d = b;

So, this has nothing to do with the approach. You can achieve this with both the approach.

Both the approach has nothing to do with public or private. They are known as Access Modifiers. Public properties / variables can be accessed from anywhere whereas the private properties / variables can be accessed from only within the block it's defined.

Well, then why properties have been introduced? What's the use? The answer is, they have been introduced to implement encapsulation.

Let's say, you have

private int _unitprice;
private int _quantity;

public int UnitPrice
{
    get 
    { 
         return _unitprice; 
    }
    set 
    { 
        if(value > 0) _unitprice = value 
    }
}

public int Quantity
{
    get 
    { 
         return _quantity; 
    }
    set 
    { 
        if(value > 0) _quantity = value 
    }
}

public int Total
{
    get 
    { 
         return _unitprice * _quantity; 
    }
}

Say, you don't want users to access private members _unitprice and _quantity directly, instead access them using public facing properties with / without any validation. I did some validation in the example above, but it's not mandatory.

And some other properties like Quantity which doesn't exists say in table, but we have provided in Business Class for providing the facility and simplicity to the user.

Also, you may notice that Total have only get and no set. So you can even restrict assignment or retrieval using getters and setters. Hope that helps :)

0

The simple way to explain your question regard private and public is; private stands for privacy and public stand for spotlight.

When you declare a private field or method your intention is to make it visible to where its declares only, outside of the form or class is impossible to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.