new int() 被看作int.Int32() InitializeComponent(); int i; i = 3; int j = new int(); j = 3; label1.Text= i.GetType().ToString(); label2.Text = j.GetType().ToString(); [code=MSIL] .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { // 代码大小 73 (0x49) .maxstack 2 .locals init ([0] int32 i, [1] int32 j) IL_0000: ldarg.0 IL_0001: call instance void [System.Windows.Forms]System.Windows.Forms.Form::.ctor() IL_0006: ldarg.0 IL_0007: call instance void WinFormDemo.NewIntDemo::InitializeComponent() IL_000c: ldc.i4.3 IL_000d: stloc.0 IL_000e: ldc.i4.0 IL_000f: stloc.1 IL_0010: ldc.i4.3 IL_0011: stloc.1 IL_0012: ldarg.0 IL_0013: ldfld class [System.Windows.Forms]System.Windows.Forms.Label WinFormDemo.NewIntDemo::label1 IL_0018: ldloc.0 IL_0019: box [mscorlib]System.Int32 IL_001e: call instance class [mscorlib]System.Type [mscorlib]System.Object::GetType() IL_0023: callvirt instance string [mscorlib]System.Object::ToString() IL_0028: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string) IL_002d: ldarg.0 IL_002e: ldfld class [System.Windows.Forms]System.Windows.Forms.Label WinFormDemo.NewIntDemo::label2 IL_0033: ldloc.1 IL_0034: box [mscorlib]System.Int32 IL_0039: call instance class [mscorlib]System.Type [mscorlib]System.Object::GetType() IL_003e: callvirt instance string [mscorlib]System.Object::ToString() IL_0043: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string) IL_0048: ret } // end of method NewIntDemo::.ctor[/code]
int 是值类型,不是 引用类型[Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)] public struct Int32 : IComparable, IFormattable, IConvertible, IComparable<int>, IEquatable<int> { public const int MaxValue = 0x7fffffff; public const int MinValue = -2147483648; internal int m_value; public int CompareTo(object value); public int CompareTo(int value); public override bool Equals(object obj); public bool Equals(int obj); public override int GetHashCode(); public override string ToString(); public string ToString(string format); public string ToString(IFormatProvider provider); public string ToString(string format, IFormatProvider provider); public static int Parse(string s); public static int Parse(string s, NumberStyles style); public static int Parse(string s, IFormatProvider provider); public static int Parse(string s, NumberStyles style, IFormatProvider provider); public static bool TryParse(string s, out int result); public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out int result); public TypeCode GetTypeCode(); bool IConvertible.ToBoolean(IFormatProvider provider); char IConvertible.ToChar(IFormatProvider provider); sbyte IConvertible.ToSByte(IFormatProvider provider); byte IConvertible.ToByte(IFormatProvider provider); short IConvertible.ToInt16(IFormatProvider provider); ushort IConvertible.ToUInt16(IFormatProvider provider); int IConvertible.ToInt32(IFormatProvider provider); uint IConvertible.ToUInt32(IFormatProvider provider); long IConvertible.ToInt64(IFormatProvider provider); ulong IConvertible.ToUInt64(IFormatProvider provider); float IConvertible.ToSingle(IFormatProvider provider); double IConvertible.ToDouble(IFormatProvider provider); decimal IConvertible.ToDecimal(IFormatProvider provider); DateTime IConvertible.ToDateTime(IFormatProvider provider); object IConvertible.ToType(Type type, IFormatProvider provider); } Expand Methods
我时这样理解的:封装的就是引用类型,直接定义的就是值类型
也就是有New的时引用类型
http://topic.csdn.net/u/20081028/15/D1883CBA-7777-457E-AAA7-974AF565FF4C.html
看看空军的解释。
没想到这个问题有这么多人不懂啊
j = 3; label1.Text= i.GetType().ToString(); label2.Text = j.GetType().ToString();
[code=MSIL]
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// 代码大小 73 (0x49)
.maxstack 2
.locals init ([0] int32 i,
[1] int32 j)
IL_0000: ldarg.0
IL_0001: call instance void [System.Windows.Forms]System.Windows.Forms.Form::.ctor()
IL_0006: ldarg.0
IL_0007: call instance void WinFormDemo.NewIntDemo::InitializeComponent()
IL_000c: ldc.i4.3
IL_000d: stloc.0
IL_000e: ldc.i4.0
IL_000f: stloc.1
IL_0010: ldc.i4.3
IL_0011: stloc.1
IL_0012: ldarg.0
IL_0013: ldfld class [System.Windows.Forms]System.Windows.Forms.Label WinFormDemo.NewIntDemo::label1
IL_0018: ldloc.0
IL_0019: box [mscorlib]System.Int32
IL_001e: call instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
IL_0023: callvirt instance string [mscorlib]System.Object::ToString()
IL_0028: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string)
IL_002d: ldarg.0
IL_002e: ldfld class [System.Windows.Forms]System.Windows.Forms.Label WinFormDemo.NewIntDemo::label2
IL_0033: ldloc.1
IL_0034: box [mscorlib]System.Int32
IL_0039: call instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
IL_003e: callvirt instance string [mscorlib]System.Object::ToString()
IL_0043: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string)
IL_0048: ret
} // end of method NewIntDemo::.ctor[/code]
public struct Int32 : IComparable, IFormattable, IConvertible, IComparable<int>, IEquatable<int>
{
public const int MaxValue = 0x7fffffff;
public const int MinValue = -2147483648;
internal int m_value;
public int CompareTo(object value);
public int CompareTo(int value);
public override bool Equals(object obj);
public bool Equals(int obj);
public override int GetHashCode();
public override string ToString();
public string ToString(string format);
public string ToString(IFormatProvider provider);
public string ToString(string format, IFormatProvider provider);
public static int Parse(string s);
public static int Parse(string s, NumberStyles style);
public static int Parse(string s, IFormatProvider provider);
public static int Parse(string s, NumberStyles style, IFormatProvider provider);
public static bool TryParse(string s, out int result);
public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out int result);
public TypeCode GetTypeCode();
bool IConvertible.ToBoolean(IFormatProvider provider);
char IConvertible.ToChar(IFormatProvider provider);
sbyte IConvertible.ToSByte(IFormatProvider provider);
byte IConvertible.ToByte(IFormatProvider provider);
short IConvertible.ToInt16(IFormatProvider provider);
ushort IConvertible.ToUInt16(IFormatProvider provider);
int IConvertible.ToInt32(IFormatProvider provider);
uint IConvertible.ToUInt32(IFormatProvider provider);
long IConvertible.ToInt64(IFormatProvider provider);
ulong IConvertible.ToUInt64(IFormatProvider provider);
float IConvertible.ToSingle(IFormatProvider provider);
double IConvertible.ToDouble(IFormatProvider provider);
decimal IConvertible.ToDecimal(IFormatProvider provider);
DateTime IConvertible.ToDateTime(IFormatProvider provider);
object IConvertible.ToType(Type type, IFormatProvider provider);
}
Expand Methods