473,379 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Public data members vs get/set properties

Perhaps I'm missing something obvious, but I've been curious about one of
the coding practices I see advocated. I'm a longtime C/C++ programmer
trying to learn C#, and I started looking around for coding standards/best
practices.

Several of the documents I've read require that all members be private (or
protected), and the get/set properties be provided for any elements exposed
to the outside world.

In C++ this makes perfect sense, because method syntax differs from member
syntax. Making a member variable public ties your hands, because if you
ever want to change the behavior of the member and replace it with a public
method, you break existing code.

However, one of the beauties of properties in C# is that consumers of a
class use member syntax to manipulate a property. I can declare a public
member variable and lets consumers use it. Then in the future I discover
that the implementation needs to change and that instead of permitting
direct access to a member I want to add validation, or update dependent
data, fire an event, etc. To do that I simply rename the member, make it
private, and implement get/set properties that conform to the semantics of
the old member. These of course are methods internally, so I can place any
needed logic in the method. I have to rename the member inside my class,
but the external interface doesn't change at all.

From my C++ background I have a knee jerk reaction that public data members
in a class are evil, but objectively I don't see why this is necessarily the
case in C#.

Obvious caveats:
1. If a coding standard dictates that members have a different naming
convention from properties, then it is necessary to have both.
2. If the property get/set methods are more than simple accessors that read
and write a variable, then it is necessary to use the properties. But I'd
bet that many, many properties exist that are nothing more than a single
assignment or return.

Am I missing a more fundamental reason to use properties instead of public
data members in C#?

Joe
Nov 17 '05 #1
3 10862
Joe,

Syntactically, the calls are the same, however, on the IL level, they
are vastly different. To set the value for a field, you have an IL
instruction stfld (I believe), which sets the appropriate field with the
value at the top of the stack. With setting a property, its a method call.

While it looks the same in the code, they boil down to two very
different things. Also, when you change to a property, you change the
signature of the type, and anything that is setting that field to something
will break.

You are right, many implementations of properties are nothing more than
a set to a private field and a return. However, if you ever need to change
to a property to introduce a more complex implementation, it's better to
have that done sooner than later. This is the reason for the recommendation
in the guidelines published by MS.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe Fromm" <Jo*******@Pearson.com> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
Perhaps I'm missing something obvious, but I've been curious about one of
the coding practices I see advocated. I'm a longtime C/C++ programmer
trying to learn C#, and I started looking around for coding standards/best
practices.

Several of the documents I've read require that all members be private (or
protected), and the get/set properties be provided for any elements
exposed
to the outside world.

In C++ this makes perfect sense, because method syntax differs from member
syntax. Making a member variable public ties your hands, because if you
ever want to change the behavior of the member and replace it with a
public
method, you break existing code.

However, one of the beauties of properties in C# is that consumers of a
class use member syntax to manipulate a property. I can declare a public
member variable and lets consumers use it. Then in the future I discover
that the implementation needs to change and that instead of permitting
direct access to a member I want to add validation, or update dependent
data, fire an event, etc. To do that I simply rename the member, make it
private, and implement get/set properties that conform to the semantics of
the old member. These of course are methods internally, so I can place
any
needed logic in the method. I have to rename the member inside my class,
but the external interface doesn't change at all.

From my C++ background I have a knee jerk reaction that public data
members
in a class are evil, but objectively I don't see why this is necessarily
the
case in C#.

Obvious caveats:
1. If a coding standard dictates that members have a different naming
convention from properties, then it is necessary to have both.
2. If the property get/set methods are more than simple accessors that
read
and write a variable, then it is necessary to use the properties. But I'd
bet that many, many properties exist that are nothing more than a single
assignment or return.

Am I missing a more fundamental reason to use properties instead of public
data members in C#?

Joe

Nov 17 '05 #2
>
While it looks the same in the code, they boil down to two very
different things. Also, when you change to a property, you change the
signature of the type, and anything that is setting that field to something will break.


Ah, that makes sense. So public properties maintain source compatibility,
but not binary compatibility.
Nov 17 '05 #3
Joe Fromm <Jo*******@Pearson.com> wrote:
While it looks the same in the code, they boil down to two very
different things. Also, when you change to a property, you change the
signature of the type, and anything that is setting that field to

something
will break.


Ah, that makes sense. So public properties maintain source compatibility,
but not binary compatibility.


They don't even maintain source compatibility - you can pass a field by
reference, but not a property (in C#, anyway). Also, anything using
reflection will see the difference, of course.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Zap | last post by:
Widespread opinion is that public data members are evil, because if you have to change the way the data is stored in your class you have to break the code accessing it, etc. After reading this...
3
by: ectoplasm | last post by:
I'd like to ask for advice on the following. I have a data structure of network elements ('NetworkElement' base class & derived classes like Server, Area, Cell, etc.) The network elements are...
8
by: Allen Anderson | last post by:
Just curious if there is a way to hide a public member inherited from another class. I know you can 'new' to give it your own version, but is there a way to make it no longer public period? I...
6
by: darrel | last post by:
I'm still not quite sure how best to handle the passing of data between controls. This is a method I'm using at the moment: I have an XML file that contains a variety of page-centric...
4
by: louise raisbeck | last post by:
Resending this as own topic as didnt get answer from original. Would be grateful for a response from anyone that knows. Thanks. Hi there, I found your post really helpful..but i wondered if, once...
18
by: Joel Hedlund | last post by:
Hi! The question of type checking/enforcing has bothered me for a while, and since this newsgroup has a wealth of competence subscribed to it, I figured this would be a great way of learning...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
86
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
0
by: pvishweshwar | last post by:
Hi All, am developing one application in which i have to retrieve the msolap dimension members properties data.In this till now am able to get the member values properly.But the problem is am...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.