params、explicit、可选参数和implicit的使用

/*
params、explicit、可选参数和implicit的使用
explicit 显示的
implicit 隐士的
params 可变参数必须是参数列表最后一个
*/
using System;

namespace Frank
{
	public class Test
    {
		public int Count{get;set;}
        public static void Main(string[] args)
        {
			Test t = new Test();
			t.Count = 10;
			Test2 t2 = new Test2();
			t2.Count = 20;

			Test2 t3 = t;//把Test类型的转换成Test2不需要显示
			Test t4 = (Test)t2;//把Test2类型转换成Test类型需要显示
			System.Console.WriteLine(t3.Count+"----"+t4.Count);
			Print("1","2","3");
			Print2(1);

          

int i = t;
float f = (float)t;

System.Console.WriteLine(i+"---"+f);//10---10

        }
		public static implicit operator Test2(Test t)//隐士转换,把Test转换成Test2
		{
			return new Test2{Count = t.Count};
		}
		public static void Print(string str,params string[] str2)//可变参数的使用
		{
			System.Console.WriteLine(str+"---"+str2);
		}
		public static void Print2(int i,string str = "1",string str2 = "2")//可选参数的使用
		{
			System.Console.WriteLine(i+"---"+str+"---"+str2);
		}

public static implicit operator int(Test t)//隐士转换,把Test转换成int
{
return t.Count;
}
public static explicit operator float(Test t)//显士转换,把Test转换成float
{
return t.Count+0.00F;
}

	}
	public class Test2
	{
		public int Count{get;set;}
		public static explicit operator Test(Test2 t)//把Test2显示转换成Test类型
		{
			return new Test{Count = t.Count};
		}
	}
}

  

posted on 2013-11-28 10:46  wp456  阅读(224)  评论(0)    收藏  举报

导航