隐式转换必须显式使用
Implicit conversion Must be used Explicitly
我有一个声明了以下运算符的结构:
public struct myStruct {
public static implicit operator int(Nullable<myStruct> m){
/*...*/
}
}
仅此运算符就可以让我将 non-nullable 结构隐式转换为 int,但尝试隐式转换其可为 null 的对应项仍然会引发编译错误:
Cannot implicitly convert type myStruct?
to int
. An explicit
conversion exists (are you missing a cast?)
显然提到的 "explicit" 运算符实际上是我声明的隐式运算符,完全删除此运算符也删除了对显式运算符的提及。
当涉及到可空结构时,为什么我被迫显式地使用这个运算符,即使它被声明为隐式的?
编辑:
所以这里是 "full code",去掉了不会使编译器错误消失的所有内容。结构在字面上保持不变,所有新的都是我的测试代码:
using System;
public struct boilDown {
public static implicit operator int(Nullable<boilDown> s) { return 0; }
} // END Struct
public class Sandbox {
void Update ()
{
boilDown nonNullable = new boilDown ();
Nullable<boilDown> NullableVersion = new Nullable<boilDown>();
int MyInt;
MyInt = nonNullable; // this work thanks to my operator
MyInt = NullableVersion; // But this line requires an explicit cast
}
}
版本:
你们都暗示我有一个 c# 版本问题。
我正在研究 Unity 2017.1.0f3,它使用 Mono 2.0.50727.1433 而不是 .Net。 (这显然是一个 NET3.5 等价物,但即使是他们的实验性 NET4.6 等价物也有这个问题。)
我会问他们这个问题,看看他们怎么说。
您可以明确地将 NullableVersion
转换为 int
,如下所示。
using System;
public struct boilDown {
public static implicit operator int(Nullable<boilDown> s) { return 0; }
} // END Struct
public class Sandbox {
static void Main()
{
}
void Update ()
{
boilDown nonNullable = new boilDown ();
Nullable<boilDown> NullableVersion = new Nullable<boilDown>();
int MyInt;
MyInt = nonNullable; // this work thanks to my operator
MyInt = (int)NullableVersion; // works now
}
}
感谢所有告诉我这段代码应该编译的人。
Unity 确认此错误是他们端的错误。
我有一个声明了以下运算符的结构:
public struct myStruct {
public static implicit operator int(Nullable<myStruct> m){
/*...*/
}
}
仅此运算符就可以让我将 non-nullable 结构隐式转换为 int,但尝试隐式转换其可为 null 的对应项仍然会引发编译错误:
Cannot implicitly convert type
myStruct?
toint
. An explicit conversion exists (are you missing a cast?)
显然提到的 "explicit" 运算符实际上是我声明的隐式运算符,完全删除此运算符也删除了对显式运算符的提及。
当涉及到可空结构时,为什么我被迫显式地使用这个运算符,即使它被声明为隐式的?
编辑:
所以这里是 "full code",去掉了不会使编译器错误消失的所有内容。结构在字面上保持不变,所有新的都是我的测试代码:
using System;
public struct boilDown {
public static implicit operator int(Nullable<boilDown> s) { return 0; }
} // END Struct
public class Sandbox {
void Update ()
{
boilDown nonNullable = new boilDown ();
Nullable<boilDown> NullableVersion = new Nullable<boilDown>();
int MyInt;
MyInt = nonNullable; // this work thanks to my operator
MyInt = NullableVersion; // But this line requires an explicit cast
}
}
版本:
你们都暗示我有一个 c# 版本问题。
我正在研究 Unity 2017.1.0f3,它使用 Mono 2.0.50727.1433 而不是 .Net。 (这显然是一个 NET3.5 等价物,但即使是他们的实验性 NET4.6 等价物也有这个问题。)
我会问他们这个问题,看看他们怎么说。
您可以明确地将 NullableVersion
转换为 int
,如下所示。
using System;
public struct boilDown {
public static implicit operator int(Nullable<boilDown> s) { return 0; }
} // END Struct
public class Sandbox {
static void Main()
{
}
void Update ()
{
boilDown nonNullable = new boilDown ();
Nullable<boilDown> NullableVersion = new Nullable<boilDown>();
int MyInt;
MyInt = nonNullable; // this work thanks to my operator
MyInt = (int)NullableVersion; // works now
}
}
感谢所有告诉我这段代码应该编译的人。
Unity 确认此错误是他们端的错误。