为什么我不能对结构使用 as 关键字?
我定义了以下结构:
public struct Call
{
public SourceFile caller;
public SourceFile callee;
public Call(SourceFile caller, SourceFile callee)
{
this.caller = caller;
this.callee = callee;
}
}
后来,我将它分配给另一个对象的 Tag 属性:
line.Tag = new Call(sf1, sf2);
但是当我尝试像这样检索 Tag 属性时,
Call call = line.Tag as Call;
Visual Studio 给出以下编译时错误:
运算符 as 必须在 a 中使用 引用类型或可为空类型
是什么意思?我该如何解决它?
I defined the following struct:
public struct Call
{
public SourceFile caller;
public SourceFile callee;
public Call(SourceFile caller, SourceFile callee)
{
this.caller = caller;
this.callee = callee;
}
}
Later, I assign it to the Tag property of another object:
line.Tag = new Call(sf1, sf2);
But when I try to retrieve the Tag property like so,
Call call = line.Tag as Call;
Visual Studio gives the following compile-time error:
The operator as must be used within a
reference type or nullable type
What is the meaning of that? And how can I solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一些现有的答案不太正确。您不能将不可空类型与
as
一起使用,因为如果第一个操作数是,则as
的结果是该类型的空值实际上不是合适的类型。但是,您可以将
as
与值类型一起使用...如果它们可以为空:那么您可以使用:
然后您可以将其用作:
但有两个警告:
is
后跟强制转换要慢。Call
类型:我强烈建议你将它变成一个类 - 无论如何,这个问题都会消失。
另一个想法:如果标签应该始终是
Call
,那么最好将其强制转换:这样,如果数据与您的期望不符(即存在一些错误)例如,
Tag
不是Call
),那么您就可以尽早发现它,而不是在您可能完成其他一些工作之后。请注意,根据Call
是结构体还是类,此转换的行为会有所不同,如果Tag
为 null - 您可以将 null 值转换为引用类型的变量(或可为 null 的值类型),但不适用于不可为 null 的值类型。Some of the existing answers aren't quite right. You can't use non-nullable types with
as
, because the result ofas
is the null value of the type if the first operand isn't actually of an appropriate type.However, you can use
as
with value types... if they're nullable:So you could use:
Then you can use it as:
Two caveats though:
is
followed by a castCall
type:I would strongly suggest you make it a class instead - at which point this problem goes away anyway.
Another thought: if the tag should always be a
Call
, then it's better to cast it:That way, if the data doesn't match your expectation (i.e. there's some bug such that the
Tag
isn't aCall
) then you get to find out about it early, rather than after you've potentially done some other work. Note that this cast will behave differently depending on whetherCall
is a struct or a class, ifTag
is null - you can cast a null value to a variable of a reference type (or a nullable value type), but not to a non-nullable value type.结构体是一种值类型,因此它不能与
as
运算符一起使用。如果转换失败,as
运算符必须能够分配 null 值。这仅适用于引用类型或可为空值类型。有多种方法可以解决此问题,但最好的选择是将 Call 类型从结构更改为类。这实际上会将您的类型从值类型更改为引用类型,这允许 as 运算符在转换失败时分配 null 值。
有关值类型与引用类型的更多信息,这篇是一篇不错的文章。另外,请查看 MSDN:
A struct is a value type, so it cannot be used with the
as
operator. Theas
operator must be able to assign a value of null if the cast fails. This is only possible with a reference type or a nullable value type.There are a couple ways to solve this, but your best bet is to change your
Call
type from a struct to a class. This will essentially change your type from a value type to a reference type, which allows theas
operator to assign a value of null if the cast fails.For more information on value types vs. reference types, this is a decent article. Also, have a look on MSDN:
来自 C# 规范
引用和可为 null 的类型可以为 null。 Stucts 是值类型,因此它们不能为 null。
From the C# Spec
References and nullable types can be null. Stucts are value types so they can't be null.
这是 C# 的限制。如果类型是引用类型,那么如果转换失败,它将简单地返回“null”,但由于它是值类型,因此它不知道转换失败时要返回什么。
您必须将 as 的使用替换为两个:“is”和“as”
It's a limitation of C#. If the type were a reference type, then if the cast failed it would simply return 'null', but since it's a value type, it doesn't know what to return when the cast fails.
You must replace your use of as with two: 'is' and 'as'
含义是什么 - 如前所述,结构是值类型。
我该如何解决它 - 将其更改为
What is the meaning - As stated, structures are value types.
How can I solve it - Change it to