与 C# 枚举和显式转换的混淆

发布于 2024-08-01 17:33:12 字数 1674 浏览 4 评论 0原文

我有以下枚举 -->

    public enum SyncStatus
    {
        Unavailable = 0,
        Checking = 5,
        StartedAspNetDb = 10,
        FinishedAspNetDb = 20,
        StartedMatrixDb = 30,
        FinishedMatrixDb = 40,
        StartedConnectDb = 50,
        FinishedConnectDb = 60,
        StartedCmoDb = 70,
        FinishedCmoDb = 80,
        StartedMcpDb = 90,
        FinishedMcpDb = 100
    }

我在这里使用的 -->

        SyncInBackground.ReportProgress(SyncStatus.StartedAspNetDb);
        MergeRepl aspnetdbMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "aspnetdb", "aspnetdb", "aspnetdb");
        aspnetdbMergeRepl.RunDataSync();
        SyncInBackground.ReportProgress(SyncStatus.FinishedAspNetDb);

        SyncInBackground.ReportProgress(SyncStatus.StartedMatrixDb);
        MergeRepl matrixMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "MATRIX", "MATRIX", "MATRIX");
        matrixMergeRepl.RunDataSync();
        SyncInBackground.ReportProgress(SyncStatus.FinishedMatrixDb);

        SyncInBackground.ReportProgress(SyncStatus.StartedConnectDb);
        MergeRepl connectMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "CONNECT", "Connect", "Connect");
        connectMergeRepl.RunDataSync();
        SyncInBackground.ReportProgress(SyncStatus.FinishedConnectDb); 

我不明白的是,如果 int 是默认枚举管理类型,为什么我必须 CAST 这行代码,就像这样 -->

 SyncInBackground.ReportProgress((int)SyncStatus.Checking);

原谅我的无知,我只是喜欢了解事物的原因,而不仅仅是它们如此。

I have the following enum -->

    public enum SyncStatus
    {
        Unavailable = 0,
        Checking = 5,
        StartedAspNetDb = 10,
        FinishedAspNetDb = 20,
        StartedMatrixDb = 30,
        FinishedMatrixDb = 40,
        StartedConnectDb = 50,
        FinishedConnectDb = 60,
        StartedCmoDb = 70,
        FinishedCmoDb = 80,
        StartedMcpDb = 90,
        FinishedMcpDb = 100
    }

Which I use here -->

        SyncInBackground.ReportProgress(SyncStatus.StartedAspNetDb);
        MergeRepl aspnetdbMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "aspnetdb", "aspnetdb", "aspnetdb");
        aspnetdbMergeRepl.RunDataSync();
        SyncInBackground.ReportProgress(SyncStatus.FinishedAspNetDb);

        SyncInBackground.ReportProgress(SyncStatus.StartedMatrixDb);
        MergeRepl matrixMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "MATRIX", "MATRIX", "MATRIX");
        matrixMergeRepl.RunDataSync();
        SyncInBackground.ReportProgress(SyncStatus.FinishedMatrixDb);

        SyncInBackground.ReportProgress(SyncStatus.StartedConnectDb);
        MergeRepl connectMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "CONNECT", "Connect", "Connect");
        connectMergeRepl.RunDataSync();
        SyncInBackground.ReportProgress(SyncStatus.FinishedConnectDb); 

What I don't understand is why, if int is default enum governing type, do I have to CAST this line, like so -->

 SyncInBackground.ReportProgress((int)SyncStatus.Checking);

Forgive my ignorance I just like to understand the why of things not just that they are so.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北笙凉宸 2024-08-08 17:33:12

只是没有从枚举类型到其基础类型的隐式转换。 这使得意外使用枚举作为其数值变得更加困难。

(同样没有其他方式的转换。)

顺便说一下,有一个从常量 0 到任何枚举类型的隐式转换。

哦,从装箱枚举值拆箱到其基础类型 - 反之亦然 - 也可以。 至少在某一时刻,CLI 规范和 C# 规范都严重破坏了这一点; 现在很可能已经修复了:)

编辑:

如果您实际上只想将值用作数字,那么这里有一个替代选项:

public static class SyncStatus
{
    public const int Unavailable = 0;
    public const int Checking = 5;
    public const int StartedAspNetDb = 10;
    public const int FinishedAspNetDb = 20;
    public const int StartedMatrixDb = 30;
    public const int FinishedMatrixDb = 40;
    public const int StartedConnectDb = 50;
    public const int FinishedConnectDb = 60;
    public const int StartedCmoDb = 70;
    public const int FinishedCmoDb = 80;
    public const int StartedMcpDb = 90;
    public const int FinishedMcpDb = 100;
}

或者,编写如下方法:

static void ReportProgress(SyncStatus status)
{
    SyncInBackground.ReportProgress((int) status);
}

There just isn't an implicit conversion from an enum type to its underlying type. This makes it harder to accidentally use an enum as its numeric value.

(There's likewise no conversion the other way.)

There is an implicit conversion from the constant 0 to any enum type, by the way.

Oh, and unboxing from a boxed enum value to its underlying type - or vice versa - works too. At least at one point this was really badly mangled in both the CLI spec and the C# spec; it may well have been fixed by now :)

EDIT:

Here's an alternative option if you actually just want to use the values as numbers:

public static class SyncStatus
{
    public const int Unavailable = 0;
    public const int Checking = 5;
    public const int StartedAspNetDb = 10;
    public const int FinishedAspNetDb = 20;
    public const int StartedMatrixDb = 30;
    public const int FinishedMatrixDb = 40;
    public const int StartedConnectDb = 50;
    public const int FinishedConnectDb = 60;
    public const int StartedCmoDb = 70;
    public const int FinishedCmoDb = 80;
    public const int StartedMcpDb = 90;
    public const int FinishedMcpDb = 100;
}

Alternatively, write a method like this:

static void ReportProgress(SyncStatus status)
{
    SyncInBackground.ReportProgress((int) status);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文