Using C # Advanced parameters out of

C # has three advanced parameters, namely, out, ref, params. This article, the first to introduce the use of out parameters.

out, for returning the excess value in the process. (Can be understood as a return value of different types so that a method)

We have to understand by example example Function: a method to determine whether the user login success (Boolean), and prompt the user whether successful landing (string type)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace blog
{
    class Program
    {
        static void Main(string[] args)
        {
            string str;
            Console.WriteLine("请输入用户名");
            string uersname = Console.ReadLine();
            Console.WriteLine("请输入密码");
            string password = Console.ReadLine();
            //传入参数也一样要在参数前面添加一个out
            bool b = login(uersname, password, out str);
            if (b)
            {
                Console.WriteLine(str);
            }
            else
            {
                Console.WriteLine(str);
            }
            Console.ReadKey();
        }
        public static bool login(string name, string pwd, out string msg)
        {
            //如果需要返回多个参数,则添加多个参数即可,例如login(string name, string pwd, out string msg,out int num)
            //out多余返回值,用于一个方法中多余返回的值,例如这个方法中,
            //返回值是布尔类型,同时,还可以返回一个多余的值,msg
            //out的参数必须在方法中进行初始化
            bool result;
            if (name == "admin" && pwd == "123")
            {
                msg = "登陆成功";
                result = true;

            }
            else
            {
                msg = "登陆失败";
                result = false;
            }

            return result;
        }
    }
}
Original: http: //www.cnblogs.com/linfenghp/p/6618580.html

Published 117 original articles · won praise 4 · views 80000 +

Guess you like

Origin blog.csdn.net/qq_36266449/article/details/78383642