Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

public variables vs private variables with accessors

Tags:

c#

oop

accessor

Has anyone else seen people do this:

private string _name;
public string Name{ get{ return _name; } set{ _name = value;}}

I understand using accessors if you are going to exercise some sort of control over how it gets set or perform some sort of function on it when there is a get. But if you are just going to do this, why not just make the variable public to begin with? Am I missing something?

like image 687
Kevin Avatar asked Oct 03 '08 18:10

Kevin


People also ask

Are accessors private or public?

An Accessor method is commonly known as a get method or simply a getter. A property of the object is returned by the accessor method. They are declared as public.

What is the difference between public and private variables?

Public variables, are variables that are visible to all classes. Private variables, are variables that are visible only to the class to which they belong. Protected variables, are variables that are visible only to the class to which they belong, and any subclasses.

Why we use properties instead of public variables?

Property always a better choice instead of public variables. Property is safe while public variables are unsafe. And you can not debug with public variables but you can do that with property. Public variables are useful.

Why variables should be defined as private and their getter and setters as public?

A private field may *still* be modified by local code. Getters and Setters are local code. So just having public Getters and Setters does not make the *field* public. You cannot access the field directly, it is still being mediated by local code that can change or reject the result of your trying to change the field.


Video Answer


2 Answers

If you make the member a public field, then you can't later refactor it into a property without changing the interface to your class. If you expose it as a property from the very beginning, you can make whatever changes to the property accessor functions that you need and the class's interface remains unchanged.

Note that as of C# 3.0, you can implement a property without creating a backing field, e.g.:

public string Name { get; set; }

This removes what is pretty much the only justification for not implementing public fields as properties in the first place.

like image 152
Robert Rossney Avatar answered Oct 15 '22 12:10

Robert Rossney


If you define a public interface with a property in assembly A, you could then use this interface in assembly B.

Now, you can change the property's implementation (maybe fetching the value from a database instead of storing it in a field). Then you can recompile assembly A, and replace an older one. Assembly B would carry on fine because the interface wouldn't have changed.

However, if you'd started off initially with a public field, and decided this wasn't suitable and wanted to change the implementation and to do that you needed to convert it to a property, then this would mean you'd have to change assembly A's public interface. Any clients of that interface (including assembly B) would also have to be recompiled and replaced to be able to work with this new interface.

So, you're better off starting with a property initially. This encapsulates the implementation of the property, leaving you free to change it in the future without having to worry what clients (including assembly B) are already out in the world using assembly A. Because, if there are any clients already out in the world making use of assembly A, changing the interface would break all clients. If they're used by another team in your company, or another company, then they are going to be not happy if you break their assemblies by changing the interface of yours!

like image 20
Scott Langham Avatar answered Oct 15 '22 13:10

Scott Langham