c#自定義型別的轉換方式operator,以及implicit(隱式)和explicit (顯示)宣告

weixin_30488085發表於2020-04-06

https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/explicit

https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/implicit

https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/operator

Vector2隱匿轉換例子

public struct Vector2 {
    public static implicit operator Vector2(Vector3 v){
        return new Vector2(v.x,v.y);
    }

    public static implicit operator Vector3(Vector2 v){
        return new Vector3(v.x,v.y,0);
    }
}

 

轉載於:https://www.cnblogs.com/kingBook/p/7026006.html

相關文章