在 C# 中,如何像枚举一样使用结构

本文关键字:一样 结构 枚举 何像 | 更新日期: 2023-09-27 18:35:04

我有一个结构,我试图像枚举一样使用:

public struct SQLDS_statementTypes
{
    public static string Select = "Select", 
        Update = "Update", Insert = "Insert", Delete = "Delete";
}

但它在此语句上抛出一个错误:"运算符 '==' 不能应用于类型为 'SQLDS_statementTypes' 和 'string' 的操作数"

if (statement == SQLDS_statementTypes.Update)

有没有办法解决这个问题?

在 C# 中,如何像枚举一样使用结构

有人正在寻找一些看起来或多或少符合您正在寻找的东西(我懒得找到链接),我当时写了这篇文章。 您可能希望更改类名以更符合您的要求。 我希望添加/删除值的配置很简单,如果不是,我可以详细说明。

public struct Group
{
    #region Code that is to be configured
    public static readonly Group Alpha = new Group("Group Alpha");
    public static readonly Group Beta = new Group("Group Beta");
    public static readonly Group Invalid = new Group("N/A");

    public static IEnumerable<Group> AllGroups
    {
        get
        {
            yield return Alpha;
            yield return Beta;
            yield return Invalid;
            //...
            //add a yield return for all instances here.
        }
    }
    #endregion
    private string value;
    /// <summary>
    /// default constructor
    /// </summary>
    //private Group()
    //{
    //    //you can make this default value whatever you want.  null is another option I considered, but you 
    //    //shouldn't have this me anything that doesn't exist as one of the options defined at the top of 
    //    //the page.
    //    value = "N/A";
    //}
    /// <summary>
    /// primary constructor
    /// </summary>
    /// <param name="value">The string value that this is a wrapper for</param>
    private Group(string value)
    {
        this.value = value;
    }
    /// <summary>
    /// Compares the Group to another group, or to a string value.
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public override bool Equals(object obj)
    {
        if (obj is Group)
        {
            return this.value.Equals(((Group)obj).value);
        }
        string otherString = obj as string;
        if (otherString != null)
        {
            return this.value.Equals(otherString);
        }
        throw new ArgumentException("obj is neither a Group nor a String");
    }
    public override int GetHashCode()
    {
        return value.GetHashCode();
    }
    /// <summary>
    /// returns the internal string that this is a wrapper for.
    /// </summary>
    /// <param name="group"></param>
    /// <returns></returns>
    public static implicit operator string(Group group)
    {
        return group.value;
    }
    /// <summary>
    /// Parses a string and returns an instance that corresponds to it.
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static Group Parse(string input)
    {
        return AllGroups.Where(item => item.value == input).FirstOrDefault();
    }
    /// <summary>
    /// Syntatic sugar for the Parse method.
    /// </summary>
    /// <param name="other"></param>
    /// <returns></returns>
    public static explicit operator Group(string other)
    {
        return Parse(other);
    }
    public override string ToString()
    {
        return value;
    }
}
为什么

不使用常规枚举? http://msdn.microsoft.com/en-us/library/sbbt4032.aspx

你不能。如果你想做这样的字符串比较,为什么不对公共成员使用静态类呢?

static class StatementTypes
{
    public static Select
    {
        get { return "Select"; }
    }
}

然后,您可以使用 StatementTypes.Select 在比较中。

错误是正确的,我认为(从错误中)statement是 SQLDS_statementTypes 类型,但 SQLDS_statementTypes.Update 是 string 类型,因此您尝试将结构与字符串进行比较,默认情况下这对 C# 毫无意义。

制作 statement 类型 string,如果您真的想这样做,它会编译,尽管我不确定为什么您一开始不只使用常规enum

您是否正在尝试获取枚举string值? 默认情况下,C# 已经执行此操作:

public enum Coordinates
{
    Cartesian,
    Polar
}
...
// x will contain the string "Cartesian"
var x = Coordinates.Cartesian.ToString();