zoukankan      html  css  js  c++  java
  • C#高级参数out的使用

    C#中有三个高级参数,分别是out,ref,params。本文章中先来介绍out参数的使用。

    out,用于在方法中返回多余值。(可以理解为让一个方法返回不同的类型值)

    我们通过例子来理解例子的功能:用一个方法,判断用户是否登陆成功(布尔类型),同时提示用户是否登陆成功(字符串类型)     

    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;
            }
        }
    }
    View Code
  • 相关阅读:
    Xshell 设置右键粘贴功能
    python中dict操作集合
    mac 设置网页字体
    博客收藏
    memcache 安装与简单使用
    mac安装homebrew
    Graphviz下载 使用
    jekyll 与hexo
    js 汉字排序
    初试gem
  • 原文地址:https://www.cnblogs.com/linfenghp/p/6618580.html
Copyright © 2011-2022 走看看