简体   繁体   中英

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)

is it a good practice (and safe) to define enums like this

C:

typedef enum
{
no_error = 0,
error_1
error_2,
...
}

C#

enum
{
no_error = 0,
error_1,
error_2,
}

All enum values are cast into Uint32 before Transfer. Can I always assume that error_1 = 1 and error_2=2 on C and C# side?

I'm using the GCC Compiler.

是的,两种语言都保证,如果您未明确给出枚举值作为整数值,则它比先前的枚举值大一个。

Yes in C# if you start an enum with 0 then consecutive enum should be consecutive numbers. In your example as no_error = 0 then error_1 would be 1. Also in C it is the same. Say for example in C,

enum DAY           
{

sunday = 0,    
monday,     
tuesday,  
wednesday,      ****/* wednesday is associated with 3 as Sunday is 0*/****  
thursday,  
friday  

} workday;  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM