我看到了很多具有以下结构的代码:
public void Blah()
{
int a = 0;
string b = "";
DateTime c = DateTime.MinValue;
bool d = false;
// ...More initializations with dummy values
// Overwrite the values in a, b, c, d, e.g. a = ReturnInt();
// Do calculations, reading the values from a, b, c, d, like DoCalculations(a);
}
一般来说,我更喜欢这样的东西:
public void Blah()
{
int a = GetInt();
string b = GetString();
DateTime c = GetDateTime();
bool d = GetBool();
// Do calculations, reading the values from a, b, c, d, like DoCalculations(a);
}
这真的有必要吗?由于额外的初始化,是否会影响性能?