Log in

View Full Version : c# .NET - basic - get & set



NDK
11-27-2009, 11:04 AM
Hi,

i'm programming for the first time in .NET (C#)

I was wondering, what is the good solution to write getters en setters?

Is this:

public void getVar(){
return m_var;
}
public String setVar(String newVar){
m_var = newVar;
}

or


public String var
{
get
{
return m_var;
}
set
{
m_var = value;
}
}

or are both solutions correct?
And if there is only one solution, can you explain why i can't use the other one?
tnx

techietim
11-27-2009, 11:28 AM
If you're coming from a language from Java, the former code is easier to work with.

However, C# handles public properties more like the second block of code. So if you want to make your code feel like it was made for C#, I'd go with using the latter pattern.