声明静态枚举时遇到问题,C#
您好,我正在尝试声明一个静态枚举,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
枚举是类型,而不是变量。因此,根据定义它们是“静态”的,您不需要关键字。
Enums are types, not variables. Therefore they are 'static' per definition, you dont need the keyword.
取出
静态
。枚举是类型,而不是成员;没有静态或非静态枚举的概念。
您可能试图创建您类型的静态字段,但这与类型声明无关。
(尽管您可能不应该创建静态字段)
另外,您不应该创建
公共
嵌套类型。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.不需要将其定义为 static。编译枚举类型时,C# 编译器将每个符号转换为 类型的常量字段。例如,编译器处理前面显示的 Color 枚举
就好像您编写了类似于以下内容的代码:
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:
枚举是一种类型,而不是值。修饰符
static
在那里没有多大意义。An enum is a type, not a value. The modifier
static
doesn't make much sense there.您正在尝试将枚举声明设为静态,即
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..Net 中的类型可以是值类型或引用类型。
值类型 ->
枚举
、结构
和内置值类型
(bool、byte、short、int、long、sbyte、ushort、uint、ulong、char ,双精度,十进制)引用类型 ->
类
、接口
、委托
、动态
和字符串
所以,如你所见枚举是类型(例如
类
和结构
、等
)。更准确地说它们是值类型。关于值类型的一个重要点是您应该能够从它们创建实例。例如,如果您无法创建用于存储 2、3 或任何数字的实例,那么作为结构(值类型)的int
有什么好处?!这是一般规则-> 您无法使用
static
修饰符创建自定义值类型(枚举
和结构
)。几点:
如果您直接在
命名空间
中编写枚举
或结构
,它们不能被标记为私有< /code> 或
protected
就像其他类型一样。它们可以只是public
或internal
,就像其他类型一样。如果您直接在
类
中编写枚举
或结构
,您可以将它们标记为私有
或< code>protected 也是如此,因为您可以将它们标记为internal
和public
。内部类型的class
类似于类型的命名空间
,只不过您也可以将内部类型标记为private
或public
。< /p>Types in .Net can be either value types or reference types.
Value types ->
enums
,structs
andbuilt-in values types
(bool, byte, short, int, long, sbyte, ushort, uint, ulong, char, double, decimal)Reference types ->
classes
,interfaces
,delegates
,dynamic
andstrings
So, as you can see enums are types(like
classes
andstructs
,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 ofint
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
andstructs
) with thestatic
modifier.Some points:
If you write your
enums
orstructs
directly in anamespace
they can not be marked asprivate
orprotected
just like other types. They can be justpublic
orinternal
just like other types.If you write your
enums
orstructs
directly in aclass
you can mark them asprivate
orprotected
too, as you can mark them asinternal
andpublic
.class
for inner types is like anamespace
for types except you can mark inner typesprivate
orpublic
too.