Re: [问题] class能否做成自制型别

楼主: evernever (Base64)   2015-09-18 21:21:20
你要的应该是 explicit (Converson Operator)
https://msdn.microsoft.com/en-us/library/xhbhezf4.aspx
public class clsBird
{
public string Name { get; set; }
public clsBird(string name)
{
this.Name = name;
}
public static explicit operator clsBird(string name)
{
return new clsBird(name);
}
}
用法
clsBird bird = (clsBird)"big bird";
P.S. 除了 explicit 之外, Conversion Operator 还有 implicit
implicit (C# Reference)
https://msdn.microsoft.com/en-us/library/z5z9kes2.aspx
要选哪一个, 就看你的需求囉

Links booklink

Contact Us: admin [ a t ] ucptt.com