Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
What is the difference between the following declarations?

Declaration 1
C#
int A;

Declaration 2
C#
int a;
public int A
{
    get { return a; }
    set { a = value; }
}

Declaration 3
C#
public int A { get; set; }
Posted
Updated 13-Apr-11 23:16pm
v5
Comments
Pong D. Panda 14-Apr-11 0:48am    
Just a tip, dont use Int32 as a member type declaration, always use the alias on your code (int is the alias of int32).

To understand that completely you should read about properties in .NET:
http://msdn.microsoft.com/en-us/library/aa288470%28v=vs.71%29.aspx[^]
http://www.devarticles.com/c/a/C-Sharp/Understanding-Properties-in-C-Sharp/[^]

Declaration 1: "A" is only private - can't access it from outside of your class.
Declaration 2: "a" is private and "A" is a property that allows accessing "a"
Declaration 3: "A" is a property without any underlying private member - so you can use both (2 and 3) to access a member of your class from outside.
 
Share this answer
 
Comments
Pong D. Panda 14-Apr-11 0:47am    
Nicely explained. Well deserved 5!
Actually, the Declaration 1 is normal variable declaration of type Int32.
Declaration 2 is a Getter Setter defined in .NET in order to achieve validated input and output of variable.
Declaration 3 is same as Declaration 2.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900