[英]C# vs C default enum Values
After many years of reading on this site (and getting many helpful solutions), it's time for me to ask a question:) 经过多年阅读本网站(并获得许多有用的解决方案)之后,是时候让我问一个问题了:)
I was wondering about the default enum values. 我想知道默认的枚举值。 I'm using enums to send error codes from an MCU to a PC (and vice versa)
我正在使用枚举将错误代码从MCU发送到PC(反之亦然)
is it a good practice (and safe) to define enums like this 定义这样的枚举是否是一种好习惯(且安全)?
C: C:
typedef enum
{
no_error = 0,
error_1
error_2,
...
}
C# C#
enum
{
no_error = 0,
error_1,
error_2,
}
All enum values are cast into Uint32
before Transfer. 传输之前,所有枚举值都将转换为
Uint32
。 Can I always assume that error_1 = 1
and error_2=2
on C and C# side? 我可以始终假设C和C#端的
error_1 = 1
和error_2=2
吗?
I'm using the GCC Compiler. 我正在使用GCC编译器。
是的,两种语言都保证,如果您未明确给出枚举值作为整数值,则它比先前的枚举值大一个。
Yes in C# if you start an enum with 0 then consecutive enum should be consecutive numbers. 如果您以0开头的枚举,则在C#中为是,则连续的枚举应为连续的数字。 In your example as no_error = 0 then error_1 would be 1. Also in C it is the same.
在您的示例中,如果no_error = 0,则error_1将为1。同样在C语言中,它是相同的。 Say for example in C,
以C为例,
enum DAY
{
sunday = 0,
monday,
tuesday,
wednesday, ****/* wednesday is associated with 3 as Sunday is 0*/****
thursday,
friday
} workday;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.