what is the difference between these two?
Your first code example is a field and your second one is a property.
A field is a class
member that its value is assigned on a class
instantiating (if it's set on class
definition), before the constructor
is been called and you don't have any control when setting or getting it:
public static int intId;
A property is a class
member, I can describe it as a special "field" which you can control on how the data will be set and get on it,in other words - Encapsulation, it's kind of a function but behaves like a field:
public int intId
{
get
{
return intId;
}
set
{
intId = value;
}
}
In your example, the int
property is using the static
int
field but you're doing a wrong use of the both:
Your field should be with a private
modifier and not static
, otherwise it's not make sense of using it because it might be change from external sources.
They both have the same name, change it.
Like that:
private int _intId;
public int IntId
{
get
{
return _intId;
}
set
{
_intId = value;
}
}