Difference Between variable and property ?

Posted by Nandkishorre under C# on 9/2/2013 | Points: 10 | Views : 7996 | Status : [Member] | Replies : 2
Hi to all,

I have confusion on property and variable.

public int ENo;

public int EMPNO
{
get ; set;
}

using above ENO variable we can read and write values, and using above EMPNO property we cam read and write values.

i don't want to set any condition to property.. if we put the condition to set property i know the difference, but here i didn't put any condition to property... then both plays same role.

What is the use of property here ? when we used these type of properties ?

Could anyone know reply to this.. ?


Regards
Nanda Kishore.CH




Responses

Posted by: Bandi on: 9/2/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
The following are the some important differences between variables and properties.

Variable:
**********
1) Declaration: Single declaration statement

2) Implementation: Single storage location

3) Storage: Directly associated with variable's value

4) Executable code: None

5) Read and write access: Read/write or read-only

6) Custom actions (in addition to accepting or returning value): Not possible


Property:
********
1) Declaration: Series of statements in a code block

2) Implementation: Executable code (property procedures)

3) Storage: Typically has internal storage not available outside the property's containing class or module.
Property's value might or might not exist as a stored element 1

4) Executable code: Must have at least one procedure

5) Read and write access: Read/write, read-only, or write-only

6) Custom actions (in addition to accepting or returning value): Can be performed as part of setting or retrieving property value


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Nandkishorre, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/2/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
This is very good questions.
Below are the points why use properties over fields, 5 reasons:

1 - Fields can’t be used in Interfaces
You can’t enforce the existence of a field in an object’s public contract through an interface. For properties though it works fine.

2 - Validation
While your application currently may not require any validation logic to set a particular value, changing business requirements may require inserting this logic later. At that point changing a field to a property is a breaking change for consumers of your API. (For example if someone was inspecting your class via reflection).

3 - Binary Serialization
Changing a field to a property is a breaking change if you’re using binary serialization. Incidentally, this is one of the reasons VB10’s auto-implemented properties have a “bindable” backing field (i.e. you can express the name of the backing field in code) – that way, if you change an auto-implemented property to an expanded property, you can still maintain serialization compatibility by keeping the backing field name the same (in C# you’re forced to change it because it generates backing fields with unbindable names).

4 - A lot of the .NET databinding infrastructure binds to properties but not fields
I’ve heard arguments on both sides as to whether or not that’s a good thing, but the reality is that’s the way it works right now. (Note from me: WPF bindings work on properties)

5 - Exposing a public field is an FxCop violation

Other different are:-
1. It may not be necessary today, but if it becomes necessary later it's a breaking change
2. Databinding works only on properties, not on fields (I think, not a big databinding user)
3. It allows to insert validations, logging, breakpoints when accessing the value.

You can also see the below link :

http://msdn.microsoft.com/en-us/library/sk5e8eth.aspx

Happy Coding.

Nandkishorre, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response