我的ac#枚举看起来像这样:
public enum EthernetLinkSpeed {
[Description("10BASE-T")]
_10BaseT,
[Description("100BASE-T")]
_100BaseT,
[Description("1000BASE-T")]
_1000BaseT,
[Description("Disconnected")]
Disconnected
}
Run Code Online (Sandbox Code Playgroud)
我为每个值添加了一个前导下划线,以使编译器满意.
这样的枚举是否有标准的命名约定?我用过的下划线似乎不是最好的选择.
请原谅我,如果这是一个微不足道的问题 - 我通常是一个控制系统人(plc和自动化),但最近我发现自己参与了一些嵌入式微控制器和PC项目.
假设我有一个接受指向"命令字节"数组的指针的函数,通常长度为5或10个字节,如下所示:
char cmd_i2c_read(unsigned char *cmd, unsigned short cmd_len) { ... }
Run Code Online (Sandbox Code Playgroud)
我想解码命令字节(*cmd).
这是更好的形式:
创建指示每个字节用途的局部变量:
unsigned char device_address = cmd[2];
unsigned char register_address = cmd[3];
unsigned char num_bytes = cmd[4];
// use the local variables:
if(num_bytes &le 0xFF) {
do_stuff(device_address, register_address, num_bytes);
}Run Code Online (Sandbox Code Playgroud)创建本地指针:
unsigned char *device_address = &cmd[2];
unsigned char *register_address = &cmd[3];
unsigned char *num_bytes = &cmd[4];
// use the pointers:
if(*num_bytes &le 0xFF) {
do_stuff(*device_address, *register_address, *num_bytes);
}Run Code Online (Sandbox Code Playgroud)直接索引*cmd数组:
if(cmd[4] <= 0xFF) {
do_stuff(cmd[2], cmd[3], …
我正在一个方法中执行一些参数验证,并在必要时抛出异常.我是否需要手动抛出这种类型的异常?只要调用者被包装在try..catch块中,无论手动检查是否到位,都会抛出类似的异常.
public static Int16 ToInt16(this byte[] value, int startIndex, bool isBigEndian) {
// are these exceptions necessary?
if (value == null) {
throw new ArgumentNullException("value");
}
if ((startIndex + 1) >= value.Length) {
throw new ArgumentOutOfRangeException("startIndex");
}
return (isBigEndian)
? (Int16)((value[startIndex] << 8) | value[startIndex + 1])
: (Int16)((value[startIndex + 1] << 8) | value[startIndex]);
}
Run Code Online (Sandbox Code Playgroud)
这是一种扩展方法,用于将数组中的2个字节转换为Int16,以便切换Big Endian或Little Endian转换.
我正在使用一个ItemsControl以List<byte>十六进制显示.的ItemsPanelTemplate是一个UniformGrid具有固定的列数:
<ItemsControl
HorizontalAlignment="Left"
VerticalAlignment="Top"
ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="16"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding StringFormat='\{0:X2\}'}" Margin="5,5,5,0"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
我想在每行前面加上一个'Address'列,就像你在Notepad ++'HEX-Editor'插件中看到的一样.
也就是说,因为我有16列,每行应该加上这样的前缀:
0000 [00 01 02 .... 0F]
0010 [10 11 12 .... 1F]
0020 [20 21 22 .... 2F]
......
有什么建议?