c# - 定义标志的结构

标签 c#

我想定义包含八个标志的结构。是否可以使用字节类型而不是其中的 8 个 bool 字段?我想要这样的东西

struct mystruct
{
  byte first:1;
  byte second:1;
  ...
}

我想要二进制表示来给我标志值。

如果我有一些值应该超过 1 位,e。 G。 2 或 4

最佳答案

是的,你可以不使用结构,而是使用枚举,就像这样:

[Flags]
public enum MyFlags
{
    First = 1,
    Second = 2,
    Third = 4,
    Fourth = 8
}

关于c# - 定义标志的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18591389/

相关文章:

c# - .net - 使用 C# Windows 服务访问 Linux 目录

c# - 使用C#正则表达式替换XML元素内容

c# - 由于缺少程序集,Dotnet 格式未在 Github Action 中运行

c# - 了解 System.Xml.Serialization.XmlIgnoreAttribute 与基类

c# - 小娜 VCD : set command's Navigate target to a Page in a subfolder

c# - 调整 MenuStrip 的宽度

c# - 滚动DataGrid时出现InvalidOperationException : The binding expression already belongs to a BindingGroup

c# - 存储静态单元测试变量的最佳方式是什么?

c# - 我如何使用最小起订量模拟一个集合

c# - 在 PowerShell 中使用 XAML/WPF,如何填充列表框?