Variable Properties vs Public Variables
I've read many books teaching the basics of C#,
and all of them (+ couple of ppl who told me the same thing) say that using properties of local variables instead of making methods to change private variables or just making the a public variable...
They all said it was "better programming".
But i want to understand why...
lets say i've made the following class
Code:
namespace exampleNamespace
{
class exampleClass
{
int firstVar;
int secondVar;
}
}
now, i want to let another class initiate and change the vars' values.
so what's the difference between this code
Code:
namespace exampleNamespace
{
class exampleClass
{
public int firstVar;
public int secondVar;
}
}
and this code
Code:
namespace exampleNamespace
{
class exampleClass
{
int firstVar;
public int FirstVar
{get (return firstVar)
set (firstVar = Value)}
int secondVar;
public int SecondVar
{get (return secondVar)
set (secondVar = Value)}
}
}
i know you can write only the get part - but it's just like making it readonly...
and i never really understood why you have to make the local variables private when you make methods to change them....
what's the point in that? :P
Roy