Which one is better? Declaring the member variable in the class or returning out parameters of methods?Performance aspect which one is better one.
class A{
//Declaring member variable
private string name;
private int age;
private method Display()
{
Passing();
Console.Write("{0}-{1}",name,age);
}
private void Passing()
{
name = "Hello World";
age = 21;
}
}
Or
class A{
//out parameter implementation
private method Display()
{
string name= string.Empty;
int age = 0;
Passing(out name,out age);
Console.Write("{0}-{1}",name,age);
}
private void Passing(out string name,out int age)
{
name = "Hello World";
age = 21;
}
}
Definitely, the former. Using out parameters is generally not a great idea. Don’t get me wrong, out params have their uses, but in the case of setting state like in this case don’t use them. They are good for things like TryParse where you return a bool indicating whether the parsing was successful or not and then return the result of the parse in an out param.