c# 我应该在 void 方法中使用输出吗?
c# Should I use a output in my void method?
private void button1_Click(object sender, EventArgs e)
{
try
{
int Number = Convert.ToInt32(textBox1.Text);
HalfNumber(Number);
textBox1.Focus();
textBox1.SelectAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void HalfNumber(int numberToUse)
{
double x = numberToUse / 2.0;
MessageBox.Show("Half of the number is " + x.ToString());
}
}
}
上面的代码是一个例子。
我以为我的导师说过永远不要在方法中使用输出,因为在测试代码的情况下,代码可能在世界的另一端,无法看到发生了什么。
我可能误会了她,但我希望有人解释一下
很难准确理解她的意思。然而,将对话框放在执行离散操作的函数中是出乎意料的。
return 来自 HalfNumber
方法的一个数字,然后显示包含结果的对话框会更合乎逻辑
private void button1_Click(object sender, EventArgs e)
{
try
{
int Number = Convert.ToInt32(textBox1.Text);
double myHalfNumber = HalfNumber(Number);
MessageBox.Show("Half of the number is " +myHalfNumber.ToString());
textBox1.Focus();
textBox1.SelectAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private double HalfNumber(int numberToUse)
{
return numberToUse / 2.0;
}
关注点分离
In software engineering, Separation of Concerns refers to the
delineation and correlation of software elements to achieve order
within a system. Through proper separation of concerns, complexity
becomes manageable
不要重复自己(干)
A principle of software development aimed at reducing repetition of
software patterns, replacing them with abstractions; and several
copies of the same data, using data normalization to avoid redundancy.
将 HalfNumber
放入它自己的离散方法中可以让您重用您的代码,并将逻辑分离为仅可维护和可预测的逻辑
void 表示您不能 return 任何值。
如果你想输出你需要指定它。
之后,您可以使用 return 值。
例如:
private double HalfNumber(int numberToUse)
{
double x = numberToUse / 2.0;
return x;
}
你可以这样使用
private void button1_Click(object sender, EventArgs e)
{
try
{
int Number = Convert.ToInt32(textBox1.Text);
double result = HalfNumber(Number);
MessageBox.Show("Half of the number is " + result.ToString());
textBox1.Focus();
textBox1.SelectAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private double HalfNumber(int numberToUse)
{
double x = numberToUse / 2.0;
return x;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
int Number = Convert.ToInt32(textBox1.Text);
HalfNumber(Number);
textBox1.Focus();
textBox1.SelectAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void HalfNumber(int numberToUse)
{
double x = numberToUse / 2.0;
MessageBox.Show("Half of the number is " + x.ToString());
}
}
}
上面的代码是一个例子。
我以为我的导师说过永远不要在方法中使用输出,因为在测试代码的情况下,代码可能在世界的另一端,无法看到发生了什么。
我可能误会了她,但我希望有人解释一下
很难准确理解她的意思。然而,将对话框放在执行离散操作的函数中是出乎意料的。
return 来自 HalfNumber
方法的一个数字,然后显示包含结果的对话框会更合乎逻辑
private void button1_Click(object sender, EventArgs e)
{
try
{
int Number = Convert.ToInt32(textBox1.Text);
double myHalfNumber = HalfNumber(Number);
MessageBox.Show("Half of the number is " +myHalfNumber.ToString());
textBox1.Focus();
textBox1.SelectAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private double HalfNumber(int numberToUse)
{
return numberToUse / 2.0;
}
关注点分离
In software engineering, Separation of Concerns refers to the delineation and correlation of software elements to achieve order within a system. Through proper separation of concerns, complexity becomes manageable
不要重复自己(干)
A principle of software development aimed at reducing repetition of software patterns, replacing them with abstractions; and several copies of the same data, using data normalization to avoid redundancy.
将 HalfNumber
放入它自己的离散方法中可以让您重用您的代码,并将逻辑分离为仅可维护和可预测的逻辑
void 表示您不能 return 任何值。 如果你想输出你需要指定它。 之后,您可以使用 return 值。
例如:
private double HalfNumber(int numberToUse)
{
double x = numberToUse / 2.0;
return x;
}
你可以这样使用
private void button1_Click(object sender, EventArgs e)
{
try
{
int Number = Convert.ToInt32(textBox1.Text);
double result = HalfNumber(Number);
MessageBox.Show("Half of the number is " + result.ToString());
textBox1.Focus();
textBox1.SelectAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private double HalfNumber(int numberToUse)
{
double x = numberToUse / 2.0;
return x;
}