zoukankan      html  css  js  c++  java
  • C# out和ref关键字

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace study
    {
        public class Program
        {
            /// <summary>
            /// ref关键字可以让一个值类型的输入按引用传递
            /// </summary>
            private static void refFunction(int x,ref int a)
            {
                a = a * x;
            }        
            /// <summary>
            /// out关键字的使用
            /// 使用out关键字,输入参数str在传递给outKeyWord()方法前不必进行初始化,如果没有关键字,是无法编译的
            /// </summary>
            private static void outKeyWord(out string str)
            {
                //输出参数str在方法返回前必须被赋值
                str = "a string";
            }
            static void Main(string[] args)
            {
                //***********ref关键字的使用
                int x = 12, a = 5;
                Console.WriteLine(a);//输出:5
                refFunction(x, ref a);
                Console.WriteLine(a);//输出:60
                
                //***********out关键字的使用
                string str;//不必初始化
                outKeyWord(out str);//输出:a string
                Console.WriteLine(str);
                Console.ReadKey();
    
            }
        }
    }

     原创文章,转载请注明出处:http://www.cnblogs.com/hongfei/archive/2013/01/04/2844624.html

  • 相关阅读:
    1048 石子归并
    高精度算法小结
    3117 高精度练习之乘法
    UVa 11809
    3115 高精度练习之减法
    3116 高精度练习之加法
    “da shen” in my heart
    爱是怀疑!
    普通disco
    崇拜
  • 原文地址:https://www.cnblogs.com/hongfei/p/2844624.html
Copyright © 2011-2022 走看看