C#基础-out与ref字段

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

namespace ConsoleApplication2
{
    class Program
    {
       
        static void Main(string[] args)
        {
            string outString = "This is the outString value";

            Console.WriteLine(outString);

            bool ok = outMethod(out outString);

            Console.WriteLine(outString);

            Console.WriteLine(ok.ToString());


            /*我是分隔线*/
            string refString = "This is the refString value";

            Console.WriteLine(refString);

            bool okk = refMethod(ref refString);

            Console.WriteLine(refString);

            Console.WriteLine(okk.ToString());

            Console.ReadKey();

        }
        static bool outMethod(out string str)
        {
            
            str = "This is the new outString value"; //必须设值
            return true;
        }
        static bool refMethod(ref string str)
        {

            str = "This is the new outString value"; //此句可以不写,即不用重新设值
            return true;
        }
    }
}
原文地址:https://www.cnblogs.com/lovecsharp094/p/5719761.html