不容置疑,枚举当然是值类型。不过,MSDN上有这样一句话:类 Enum 是从类 ValueType 派生而来的,即 Enum 本身是引用类型,而不是值类型。是何意思?还请高手指点一下。
解决方案 »
- 关于向ThunderRt6UserControlDC类型Button发消息的问题
- 如何在一个线程内结束掉另外的99个线程
- 急 怎么获取双击行的ID aspxgridview控件
- DropDownList 如何绑定数据库中的数据?
- 大虾们我想在IDE自动提示我自己写的方法的时候,把中文注释也提示上去,就象系统空间的方法一样,有什么好办法吗?
- 框架里怎么加控件???急!!!知道的朋友请帮帮忙
- 初学者进阶:从过程化编程转到面向对象编程
- 串口关闭出错的问题,代码如下
- 关于资源文件的问题(有一定难度)希望高手帮忙!
- C#如何获取rtsp格式的视频
- .NET中,Session没有创建,就用,程序竟然没报错,why?
- 怎样对dataset中的表进行操作
{
class Program
{
public static void OutParam( int o)
{
o = 100;
}
static void Main(string[] args) {
enum enumtype{a,b,c,d};
enumtype e = enumtype.a;
int x=0;
Program.OutParam( x);
Console.WriteLine("{0}",x);
}
}
}我也很纳闷,对这个问题?
为什么上面的会报错呢?
liuxingjia000:枚举类型不能定义在方法中吧
你看得是System.Enum这是一个class
System.Enum和
enum Test
不是同一个东西。
System.Enum和
enum Test
不是同一个东西
是说:
System.Enum 是一个类
enum Test是声明一个枚举类型
那这个枚举类型到底是什么值类型,还是应用类型
还有:结构体也不能在方法中声明?
enum Test是声明一个枚举类型:值类型
即 Enum 本身是引用类型,而不是值类型。这只是你自己的错误推断,所有的值类型都从System.ValueType继承。
但即使是这样,我们也不能创造一个类从System.ValueType继承。
[C#]
[Serializable]
public abstract class Enum : IComparable, IFormattable,
IConvertible
不管是值类型,还是引用类型,都是起源于System.ValueType,也就是说System.ValueType是所有类型的根本。
类型的根本是System.Object以下所述以.NET Framework 2.0为依据。System.Enum继承于System.ValueType。而所有的枚举类型又都继承于System.Enum。
任何继承于System.ValueType的类型都是值类型。System.Enum的原形可以参考MSDN:
http://msdn2.microsoft.com/zh-cn/library/system.enum.aspx
[ComVisibleAttribute(true)]
public abstract class Enum : ValueType, IComparable, IFormattable, IConvertible
由于它是抽象的,那么他的实例只能是引用类型的(即转换为Enum存在装箱),否则无法产生虚函数和抽象函数。这样的话MSDN上的那句话就可以理解了。
Enum是抽象的,怎么能产生实例呢?奇怪的是,Enum继承于ValueType,为什么又会变成引用类型?为什么从Enum派生出来的枚举型又会转变为值类型?越弄越糊涂,是不是应该问问Anders Hejlsberg?可惜我认得他,他不认得我
http://blog.csdn.net/Knight94/archive/2006/08/11/1050901.aspx
用enum声明的枚举是值类型,都继承自System.Enum。System.Enum重写了一些基类的方法,供其派生类(用enum定义的枚举)使用。Object.ReferenceEquals方法似乎不能测试System.Enum,因为你不能直接构造System.Enum的实例。
{Name = "aaa" FullName = "WindowsApplication1.Form1+aaa"}
[System.RuntimeType]: {Name = "aaa" FullName = "WindowsApplication1.Form1+aaa"}
base {System.Reflection.MemberInfo}: {Name = "aaa" FullName = "WindowsApplication1.Form1+aaa"}
Assembly: {WindowsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
AssemblyQualifiedName: "WindowsApplication1.Form1+aaa, WindowsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
Attributes: Public | NestedPublic | Sealed
BaseType: {Name = "Enum" FullName = "System.Enum"}
ContainsGenericParameters: false
DeclaringMethod: “(typeof(aaa)).DeclaringMethod”引发了“System.InvalidOperationException”类型的异常
DeclaringType: {Name = "Form1" FullName = "WindowsApplication1.Form1"}
FullName: "WindowsApplication1.Form1+aaa"
GenericParameterAttributes: “(typeof(aaa)).GenericParameterAttributes”引发了“System.InvalidOperationException”类型的异常
GenericParameterPosition: “(typeof(aaa)).GenericParameterPosition”引发了“System.InvalidOperationException”类型的异常
GUID: {0e329814-129c-3ef9-a6e7-d280111fb912}
HasElementType: false
IsAbstract: false
IsAnsiClass: true
IsArray: false
IsAutoClass: false
IsAutoLayout: true
IsByRef: false
IsClass: false
IsCOMObject: false
IsContextful: false
IsEnum: true
IsExplicitLayout: false
IsGenericParameter: false
IsGenericType: false
IsGenericTypeDefinition: false
IsImport: false
IsInterface: false
IsLayoutSequential: false
IsMarshalByRef: false
IsNested: true
IsNestedAssembly: false
IsNestedFamANDAssem: false
IsNestedFamily: false
IsNestedFamORAssem: false
IsNestedPrivate: true
IsNestedPublic: false
IsNotPublic: false
IsPointer: false
IsPrimitive: false
IsPublic: false
IsSealed: true
IsSerializable: true
IsSpecialName: false
IsUnicodeClass: false
IsValueType: true
IsVisible: false
MemberType: NestedType
Module: {WindowsApplication1.exe}
Namespace: "WindowsApplication1"
ReflectedType: {Name = "Form1" FullName = "WindowsApplication1.Form1"}
StructLayoutAttribute: {System.Runtime.InteropServices.StructLayoutAttribute}
TypeHandle: {System.RuntimeTypeHandle}
TypeInitializer: null
UnderlyingSystemType: {Name = "aaa" FullName = "WindowsApplication1.Form1+aaa"}
我们总是喜欢在那里空谈理论。就是不会有人去验证一下。很简单,如果是引用类型,作为参数传入到函数,并在函数内部修改,
1)如果在函数外,此类型的值改变了,就是引用类型
2)如果在函数外,此类型的值未改变了,就是值类型。public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} void ModifyDay(TimeDay day)
{
if (day != TimeDay.Evening)
{
day = TimeDay.Evening;
}
else
{
day = TimeDay.Morning;
}
} void ModifyDay2(TimeDay2 day)
{
if (day.ThisDay != TimeDay.Evening)
{
day.ThisDay = TimeDay.Evening;
}
else
{
day.ThisDay = TimeDay.Morning;
}
} private void button1_Click(object sender, EventArgs e)
{
TimeDay day = TimeDay.Afternoon; ModifyDay(day); Console.WriteLine(day); //输出Afternoon TimeDay2 day2 = new TimeDay2(TimeDay.Morning);
ModifyDay2(day2); Console.WriteLine(day2.ThisDay); //输出Evening
}
} public enum TimeDay
{
Morning = 0,
Afternoon = 1,
Evening = 2
} public class TimeDay2
{
private TimeDay day; public TimeDay2(TimeDay day)
{
this.day = day;
} public TimeDay ThisDay
{
get { return day; }
set { day = value; }
}
}
{Name = "Enum" FullName = "System.Enum"}
[System.RuntimeType]: {Name = "Enum" FullName = "System.Enum"}
base {System.Reflection.MemberInfo}: {Name = "Enum" FullName = "System.Enum"}
Assembly: {mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
AssemblyQualifiedName: "System.Enum, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
Attributes: Public | Abstract | Serializable | BeforeFieldInit
BaseType: {Name = "ValueType" FullName = "System.ValueType"}
ContainsGenericParameters: false
DeclaringMethod: “(typeof(System.Enum)).DeclaringMethod”引发了“System.InvalidOperationException”类型的异常
DeclaringType: null
FullName: "System.Enum"
GenericParameterAttributes: “(typeof(System.Enum)).GenericParameterAttributes”引发了“System.InvalidOperationException”类型的异常
GenericParameterPosition: “(typeof(System.Enum)).GenericParameterPosition”引发了“System.InvalidOperationException”类型的异常
GUID: {c43345b9-7fed-3fc7-8fc2-7b1b82bc109e}
HasElementType: false
IsAbstract: true
IsAnsiClass: true
IsArray: false
IsAutoClass: false
IsAutoLayout: true
IsByRef: false
IsClass: false
IsCOMObject: false
IsContextful: false
IsEnum: false
IsExplicitLayout: false
IsGenericParameter: false
IsGenericType: false
IsGenericTypeDefinition: false
IsImport: false
IsInterface: false
IsLayoutSequential: false
IsMarshalByRef: false
IsNested: false
IsNestedAssembly: false
IsNestedFamANDAssem: false
IsNestedFamily: false
IsNestedFamORAssem: false
IsNestedPrivate: false
IsNestedPublic: false
IsNotPublic: false
IsPointer: false
IsPrimitive: false
IsPublic: true
IsSealed: false
IsSerializable: true
IsSpecialName: false
IsUnicodeClass: false
IsValueType: false
IsVisible: true
MemberType: TypeInfo
Module: {CommonLanguageRuntimeLibrary}
Namespace: "System"
ReflectedType: null
StructLayoutAttribute: {System.Runtime.InteropServices.StructLayoutAttribute}
TypeHandle: {System.RuntimeTypeHandle}
TypeInitializer: {Void .cctor()}
UnderlyingSystemType: {Name = "Enum" FullName = "System.Enum"}对照 IsEnum、IsValueType 可以看出 enum 的独特性。
其实,个人感觉吧。C#中所有的类型其实都是引用类型的,值类型通过装箱也能变得和引用类型一样的一些性质……记得以前看书的就因为初箱折箱折腾半天
菜鸟发言,高手看了不要笑偶
public abstract class ValueType
ValueType是不是类呢?也不是
ValueType是类,IsValueType = false