Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property(with no extra processing) vs public field [duplicate]

Tags:

c#

properties

Whenever there is question about credibility of Properties, I see that most of the discussion happens around functions/methods vs properties. But I would also like to know the compelling reason to use property with associated private field vs public field directly itself, incase of most common get/set behaviors with no other processing, I mean this way

public string CustomerName;

vs

private string customerName;
public string CustomerName
{
get{return customerName;}
set(string value){this.customerName=value;}
}
like image 932
Raj Avatar asked Aug 13 '09 14:08

Raj


People also ask

What is the difference between field and property?

Properties expose fields. Fields should (almost always) be kept private to a class and accessed via get and set properties. Properties provide a level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.

What is the difference between field and property in Javascript?

A field is a variable, a property is a syntactic sugar for two methods - a getter and a setter - which get called when you access the property instead of accessing the content of the variable directly.

Can properties be private?

Properties can be marked as public , private , protected , internal , protected internal , or private protected . These access modifiers define how users of the class can access the property.

What is properties explain read only and write only properties in detail?

Properties can be read-write (they have both a get and a set accessor), read-only (they have a get accessor but no set accessor), or write-only (they have a set accessor, but no get accessor). Write-only properties are rare and are most commonly used to restrict access to sensitive data.


4 Answers

You get source/binary compatibility if you later need to add other behavior, you get to add break points, and it's just philosophically cleaner (care about the behavior, not the storage mechanism).

Note that you don't need the whole of the latter block in C# 3:

public string CustomerName { get; set; }

See my article on "Why Properties Matter" for more information.

like image 180
Jon Skeet Avatar answered Oct 19 '22 04:10

Jon Skeet


  1. You can override or at least create a "new" property in a derived class

  2. At this point people expect properties to be exposed and fields to be hidden. If someone's going to reflect over your class (its becoming more and more common with tools like Castle Windsor, NHibernate) there is a world of difference, they will likely not be checking for exposed fields.

like image 20
George Mauer Avatar answered Oct 19 '22 04:10

George Mauer


This is mostly a bug in Java. In many other languages (Python, Delphi, Groovy), the compiler will generate the getters and setters for you unless you supply the code.

This means you can use a "public" field in Groovy and the compiler will silently generate and invoke the setter/setter. If you need to do additional magic when a field is changed, you can introduce a specialized setter and everything will work.

It's one of those things where reality clashes with a design. The Java designers didn't want the compiler to do anything you can't see. What seemed like a good idea many years ago, didn't turn out too well.

like image 38
Aaron Digulla Avatar answered Oct 19 '22 04:10

Aaron Digulla


I notice one helpful use of property. If you are going to bind the collection of your object to a DataGrid or DataGridView or other bindable control, the only recognized evaluatable names are Property and not public fields.

like image 22
Nap Avatar answered Oct 19 '22 05:10

Nap