gpt4 book ai didi

c# - 为什么枚举中不允许​​字段引用(或者这是编译器错误?)

转载 作者:可可西里 更新时间:2023-11-01 08:28:09 26 4
gpt4 key购买 nike

当我使用下面的代码时:

using System;

namespace Foo {

[Flags]
public enum Bar : ulong {
None = 0x0000000000000000,
A = 0x8000000000000000,
B = 0x0000000000000001L | A,
C = 0x0000000000000002L | B,
D = 0x0000000000000004L | A,
All = A | B | C | D
}
}

单声道编译器崩溃。这是由于语义错误(语言中不允许的错误)造成的,但编译器没有注意到,还是编译器错误?

版本:Mono 2.10.8.1

我已经在 bugzilla ( https://bugzilla.xamarin.com/show_bug.cgi?id=15801 ) 提交了错误报告

最佳答案

来自 C# 语言规范,第 14.3 节:

The associated value of an enum member may not, directly or indirectly, use the value of its own associated enum member. Other than this circularity restriction, enum member initializers may freely refer to other enum member initializers, regardless of their textual position.

所以你的代码应该是正确的(因为它已经在评论中被建议),因此它应该是一个编译器错误。

关于c# - 为什么枚举中不允许​​字段引用(或者这是编译器错误?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19669127/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com