out和ref

/*
out和ref
ref 按引用传递,一般用于结构体类型和string类型,引用类型不需要ref也能修改里面的值。使用前必须初始化变量
out 输出参数,使用前可以不需要初始化参数。但离开方法体的时候必须对变量进行赋值。
*/
namespace Frank
{
	public class Test
    {
        public static void Main(string[] args)
        {
			int i = 20;
			Get(ref i);
			System.Console.WriteLine(i);//输出10
			string str = "2";
			Get(ref str);
			System.Console.WriteLine(str);//输出1
			int i2;
			Get2(out i2);
			System.Console.WriteLine(i2);//输出0
        }
		public static void Get(ref int i)
		{
			i = 10;
		}
		public static void Get(ref string str)
		{
			str = "1";
		}
		public static void Get2(out int i)
		{
			i = 0;
		}
    }
}

  

posted on 2013-11-19 14:08  wp456  阅读(153)  评论(0)    收藏  举报

导航