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