在方法中使用OUT [英] Using OUT in methods

查看:85
本文介绍了在方法中使用OUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  static void Main(string[] args)
        {
            Console.WriteLine("Choose 1 to continue or 0 to exit");
            int choice = int.Parse(Console.ReadLine());
            if (choice==0)
            {
                Console.WriteLine("Bye");
                System.Environment.Exit(0);
            }
            do
            {

                Check(out float i,"MEssage");
                Check(out float j,"MEssage");
                Check(out float k,"MEssage");

            } while (choice==1);
        }

        public static void Check(out float param, string message)
        {
            bool isNumber;
            string input;
            do
            {
                input = Console.ReadLine();
                isNumber = float.TryParse(input, out param);
            } while (!isNumber);
        }
}





我的尝试:



我试图只检查一次用户输入,然后将其作为方法调用,如果错误则不让用户继续,但是在调用方法时出现错误。我无法完全理解方法中OUT的用法。



What I have tried:

I am trying to check users input only once and then call it as a method and not letting the user to continue if it is wrong but when calling the method there is an error. I can not fully understand the usage of OUT in methods.

推荐答案

当你用 ref 调用方法时或 out 参数,包括 ref out 修饰符,但包含类型名称。



您还需要单独声明变量。

When you call a method with a ref or out parameter, you include the ref or out modifier, but you don't include the type name.

You also need to declare the variables separately.
float i, j, k;
Check(out i,"MEssage");
Check(out j,"MEssage");
Check(out k,"MEssage");

// Now you can use i, j and k...



out(C#Reference) [ ^ ]

输出参数修饰符(C#参考) [ ^ ]


这篇关于在方法中使用OUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆