声明静态枚举时遇到问题,C#

发布于 2024-10-09 23:48:50 字数 529 浏览 3 评论 0原文

您好,我正在尝试声明一个静态枚举,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Lds.CM.MyApp.Controllers
{
    public class MenuBarsController : Controller
    {
        // Menu Bar enums
        public static enum ProfileMenuBarTab { MainProfile, Edit, photoGallery }

        public ActionResult cpTopMenuBar(string tabSelected)
        {
            ...            

“ 但我收到以下错误:“修饰符‘static’对此项目无效。” 我知道这很简单,但我似乎看不到问题所在。非常感谢!

Hi I'm trying to declar a static enum like so:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Lds.CM.MyApp.Controllers
{
    public class MenuBarsController : Controller
    {
        // Menu Bar enums
        public static enum ProfileMenuBarTab { MainProfile, Edit, photoGallery }

        public ActionResult cpTopMenuBar(string tabSelected)
        {
            ...            

"
But I'm getting the following error: "The modifier 'static' is not valid for this item."
I know it's something simple but I can't seem to see the problem. Much thanks!

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

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

发布评论

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

评论(6

雪落纷纷 2024-10-16 23:48:50

枚举是类型,而不是变量。因此,根据定义它们是“静态”的,您不需要关键字。

public enum ProfileMenuBarTab { MainProfile, Edit, PhotoGallery }

Enums are types, not variables. Therefore they are 'static' per definition, you dont need the keyword.

public enum ProfileMenuBarTab { MainProfile, Edit, PhotoGallery }
仅冇旳回忆 2024-10-16 23:48:50

取出静态
枚举是类型,而不是成员;没有静态或非静态枚举的概念。

您可能试图创建您类型的静态字段,但这与类型声明无关。
(尽管您可能不应该创建静态字段)

另外,您不应该创建公共嵌套类型

Take out static.
Enums are types, not members; there is no concept of a static or non-static enum.

You may be trying to make a static field of your type, but that has nothing to do with the type declaration.
(Although you probably shouldn't be making a static field)

Also, you should not make public nested types.

錯遇了你 2024-10-16 23:48:50

不需要将其定义为 static。编译枚举类型时,C# 编译器将每个符号转换为 类型的常量字段。例如,编译器处理前面显示的 Color 枚举
就好像您编写了类似于以下内容的代码:

internal struct Color : System.Enum {
            // Below are public constants defining Color's symbols and values
            public const Color White  = (Color) 0;
            public const Color Red    = (Color) 1;
            public const Color Green  = (Color) 2;
            public const Color Blue   = (Color) 3;
            public const Color Orange = (Color) 4;
            // Below is a public instance field containing a Color variable's value
            // You cannot write code that references this instance field directly
            public Int32 value__;
}

You don't need to define it as static.When an enumerated type is compiled, the C# compiler turns each symbol into a constant field of the type . For example, the compiler treats the Color enumeration shown earlier
as if you had written code similar to the following:

internal struct Color : System.Enum {
            // Below are public constants defining Color's symbols and values
            public const Color White  = (Color) 0;
            public const Color Red    = (Color) 1;
            public const Color Green  = (Color) 2;
            public const Color Blue   = (Color) 3;
            public const Color Orange = (Color) 4;
            // Below is a public instance field containing a Color variable's value
            // You cannot write code that references this instance field directly
            public Int32 value__;
}
油饼 2024-10-16 23:48:50

枚举是一种类型,而不是值。修饰符 static 在那里没有多大意义。

An enum is a type, not a value. The modifier static doesn't make much sense there.

旧人九事 2024-10-16 23:48:50

您正在尝试将枚举声明设为静态,即 ProfileMenuBarTab 类型的字段。要在类中声明一个类(或其他任何内容),请保留静态。

You are trying to make an enum declartion static, ie a field of the type ProfileMenuBarTab. To declare a class (or whatever) in a class, leave the static out.

留蓝 2024-10-16 23:48:50

.Net 中的类型可以是值类型引用类型

值类型 -> 枚举结构内置值类型(bool、byte、short、int、long、sbyte、ushort、uint、ulong、char ,双精度,十进制)

引用类型 -> 接口委托动态字符串

所以,如你所见枚举是类型(例如结构)。更准确地说它们是值类型。关于值类型的一个重要点是您应该能够从它们创建实例。例如,如果您无法创建用于存储 2、3 或任何数字的实例,那么作为结构(值类型)的 int 有什么好处?!

这是一般规则-> 您无法使用static修饰符创建自定义值类型(枚举结构)。

几点:

  • 如果您直接在命名空间中编写枚举结构,它们不能被标记为私有< /code> 或 protected 就像其他类型一样。它们可以只是publicinternal,就像其他类型一样。

  • 如果您直接在中编写枚举结构,您可以将它们标记为私有或< code>protected 也是如此,因为您可以将它们标记为 internalpublic。内部类型的 class 类似于类型的命名空间,只不过您也可以将内部类型标记为 privatepublic。< /p>

Types in .Net can be either value types or reference types.

Value types -> enums, structs and built-in values types(bool, byte, short, int, long, sbyte, ushort, uint, ulong, char, double, decimal)

Reference types -> classes, interfaces, delegates, dynamic and strings

So, as you can see enums are types(like classes and structs, etc). more precisely they are value types. An important point about value types is that you should be able to create instances from them. For example, What is its benefit of int that is a struct (value type) if you can't create an instance of that for storing 2, 3 or any number in it?!

This is the general rule -> you cannot create custom value types (enums and structs) with the static modifier.

Some points:

  • If you write your enums or structs directly in a namespace they can not be marked as private or protected just like other types. They can be just public or internal just like other types.

  • If you write your enums or structs directly in a class you can mark them as private or protected too, as you can mark them as internal and public. class for inner types is like a namespace for types except you can mark inner types private or public too.

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