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 3 10861
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
> 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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 =...
|
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...
|
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...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |