C function return value.

I. params

Variable parameters, regardless of several parameters, must appear at the end of the parameter list. You can directly pass an array of corresponding types for variable parameters.

class Program
    {
        static void Main(string[] args)
        {
            Test("msg");
            Test("msg", 1, 2, 3);
            int[] intArry = new int[] { 1, 2, 3 };
            Test("msg", intArry);

        }
        static void Test(string msg,params int[] args)
        {

        }
    }

Two, ref

Reference passing

Three, out

The out parameter must be assigned to the out parameter in the method before use.

The out parameter cannot get the value from the argument. So in the main function, just declare the function. It is also a reference.

out is generally used in functions with multiple return values.

Adding ref out before the parameter is not overload.

 class Program
    {
        static void Main(string[] args)
        {
            Test(out int x);
            Console.WriteLine(x);
        }
       static void Test(out int x)
        {
            x = 100;
        }
    }

out instance:

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("enter one user name");
            string id = Console.ReadLine();
            Console.WriteLine("Input password");
            string psw = Console.ReadLine();
            bool isok=Login(id, psw, out string msg);
            if (isok)
            {
                Console.WriteLine(msg);
            }
            else
            {
                Console.WriteLine(msg);
            }

        }

        private static bool Login(string id, string psw, out string msg)
        {
            bool isok = false;
            if (id!="admin")
            {
                msg = "User name error";
            }
            if (psw!="123")
            {
                msg = "Password error";
            }
            else
            {
                isok = true;
                msg = "Login successfully";
            }
            return isok ;
        }
    }

Keywords: C#

Added by rubbertoad on Sun, 01 Dec 2019 16:51:25 +0200