• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

Private Variable vs Private Properties

Hi, i am working on a c# wpf application using .net framework 4.0.

I want to know which approach is better using
Private Variables
vs
Private Automated Properties.

Thanks
0
dineshwins
Asked:
dineshwins
  • 4
  • 4
  • 2
1 Solution
 
mcs0506Commented:
Hi,
It depends on the requirements if there is no need of adding extra logic or just getter setter then you can use Auto-Implemented Properties other wise formal method of private method would be preferred.

Also see,
http://msdn.microsoft.com/en-us/library/bb384054.aspx

Regards,
Dani

0
 
Rahul_GadeCommented:

It depends on the scinario where do you want to use it.
Like if you see in future you may need to modify the logic during getter setter operation use Properties instead of variables. But if you plan to use it for "ref" or "out" parameter, then go with variables.

-Rahul
0
 
dineshwinsAuthor Commented:
What about memory and performance overheads one has over other?
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
mcs0506Commented:
Hi,
Both are at same level because .net framework generates same MSIL code for both types.

Dani
0
 
dineshwinsAuthor Commented:
Its good to know that both are the same, but automated properties creates private variables automatically,
so if i use private automated properties, .net framework has to take care of Private Properties and needs to map AutoGenerated private variables for it.

but i use private variables, then they are just private variables, no mapping, no properties and i guess less overhead.

Kindly confirm
0
 
mcs0506Commented:
Hi,
Yes it is, in this way when you have private variables, you directly access these variables within class without any overhead.

Regards,

Dani
0
 
dineshwinsAuthor Commented:
So without any debate, Private variables are better than Private Automated Propeties?
0
 
mcs0506Commented:
Hi,
yah in short, if you talking about memory overhead then it is fine you can you private variable, but it is recommended that use Automated Properties even though these are private. Because by applying this accessing way we can our properties in many scenarios like composition of classes or inheritance.
So the best programming practice says do not access class data members directly.

Regards,

Dani
0
 
Rahul_GadeCommented:
The overheads of using Property over variable directly are negligible as compared to advantages of using it. e.g.
1. Synchronization of variables in multi threaded environment.
2. Modification to logic post development.
3. Simplicity in the code as it makes your code as true black box, so documentation and understanding is easy.
4. Better control over accessibility of the variable from external world e.g. you can make readonly/writeonly properly which is not possible using variables directly.

So, its up to developers judgement but it is always recommended to use properties (even auto properties) as good programming habit and exposing variables is least recommended.

While if you want to make use of variable within the same class only, you have no reason to define property for it, unless you have any plan to expose it beyond your class boundary.

-Rahul
0
 
dineshwinsAuthor Commented:
I was expecting somethig to bit/byte levels
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now