在 C# 中通过 ref 传递的参数
parameters passing by ref in C#
我是编程新手,现在正在做一些练习,但是,我无法完成一项任务(或者我不明白),我卡在第(3)号,可以你帮我 ?
这是练习和我的代码:
(1)应该有单独的转换方法
(2)应该有一个名为 ConvertSecondsToHoursMinutesSeconds 的单独方法
(3)应该有1个int参数传值和3个int参数
通过 ref
(4)应该正确地将秒转换为小时、分钟和秒
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
conversion();
}
private void ConvertSecondsToHoursMinutesSecondsMethod(long totalSeconds)
{
long hours, mins, secs, v;
hours = totalSeconds / 3600;
v = totalSeconds % 3600;
mins = v / 60;
secs = v % 60;
}
private void conversion(ref long hours, ref long secs, ref long mins)
{
long seconds = Convert.ToInt64(userInputLabel.Text);
ConvertSecondsToHoursMinutesSecondsMethod(seconds);
outputLabel.Content = $"{hours} {mins} {secs}";
}
}
您需要将接收到的 ref 参数传递给您调用的其他方法,以便它们接收正确的值。一旦方法returns.
,局部变量通常不存在
private void ConvertSecondsToHoursMinutesSecondsMethod(long totalSeconds, ref long secs, ref long mins)
{
hours = totalSeconds / 3600;
long v = totalSeconds % 3600;
mins = v / 60;
secs = v % 60;
}
private void conversion()
{
long hours, secs, mins;
long seconds = Convert.ToInt64(userInputLabel.Text);
ConvertSecondsToHoursMinutesSecondsMethod(seconds, ref hours, ref secs, ref mins);
outputLabel.Content = $"{hours} {mins} {secs}";
}
试试这个
另请参阅 https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref 以了解使用 ref
的正确方法
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Conversion();
}
private void ConvertSecondsToHoursMinutesSecondsMethod(int totalSeconds, ref long hours, ref long secs, ref long min)
{
long v;
hours = totalSeconds / 3600;
v = totalSeconds % 3600;
min = v / 60;
secs = v % 60;
}
private void Conversion()
{
long hours = 0;
long secs = 0;
long mins = 0;
int seconds = Convert.ToInt32(userInputLabel.Text);
ConvertSecondsToHoursMinutesSecondsMethod(seconds, ref hours, ref mins, ref secs);
outputLabel.Content = $"{hours} {mins} {secs}";
}
我是编程新手,现在正在做一些练习,但是,我无法完成一项任务(或者我不明白),我卡在第(3)号,可以你帮我 ? 这是练习和我的代码:
(1)应该有单独的转换方法
(2)应该有一个名为 ConvertSecondsToHoursMinutesSeconds 的单独方法
(3)应该有1个int参数传值和3个int参数 通过 ref
(4)应该正确地将秒转换为小时、分钟和秒
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
conversion();
}
private void ConvertSecondsToHoursMinutesSecondsMethod(long totalSeconds)
{
long hours, mins, secs, v;
hours = totalSeconds / 3600;
v = totalSeconds % 3600;
mins = v / 60;
secs = v % 60;
}
private void conversion(ref long hours, ref long secs, ref long mins)
{
long seconds = Convert.ToInt64(userInputLabel.Text);
ConvertSecondsToHoursMinutesSecondsMethod(seconds);
outputLabel.Content = $"{hours} {mins} {secs}";
}
}
您需要将接收到的 ref 参数传递给您调用的其他方法,以便它们接收正确的值。一旦方法returns.
,局部变量通常不存在private void ConvertSecondsToHoursMinutesSecondsMethod(long totalSeconds, ref long secs, ref long mins)
{
hours = totalSeconds / 3600;
long v = totalSeconds % 3600;
mins = v / 60;
secs = v % 60;
}
private void conversion()
{
long hours, secs, mins;
long seconds = Convert.ToInt64(userInputLabel.Text);
ConvertSecondsToHoursMinutesSecondsMethod(seconds, ref hours, ref secs, ref mins);
outputLabel.Content = $"{hours} {mins} {secs}";
}
试试这个
另请参阅 https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref 以了解使用 ref
的正确方法public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Conversion();
}
private void ConvertSecondsToHoursMinutesSecondsMethod(int totalSeconds, ref long hours, ref long secs, ref long min)
{
long v;
hours = totalSeconds / 3600;
v = totalSeconds % 3600;
min = v / 60;
secs = v % 60;
}
private void Conversion()
{
long hours = 0;
long secs = 0;
long mins = 0;
int seconds = Convert.ToInt32(userInputLabel.Text);
ConvertSecondsToHoursMinutesSecondsMethod(seconds, ref hours, ref mins, ref secs);
outputLabel.Content = $"{hours} {mins} {secs}";
}