我觉得没那么简单本人用.net reflector反射了一下int32类 发现代码部分如下 public struct Int32 : IComparable, IFormattable, IConvertible, IComparable<int>, IEquatable<int> { public const int MaxValue = 0x7fffffff; public const int MinValue = -2147483648; internal int m_value;........ 可以看到 int32是结构类型,而且他的定义中竟然包含了int。 如果int32=int,则出现循环定义的问题,这是不允许的。 求达人解释此现象。
刚买新书《CLR via C# 第三版》,P101以下四行代码都能正确编译,并生成完全相同的LI:int a = 0; System.Int32 a = 0; int a = new int(); System.Int32 a = new System.Int32() P102 从另一角度思考,可以想象C#编译器自动假定在所有源代码中添加了以下using指令:using sbyte = System.SByte; using byte = System.Byte; using short = System.Int16; using ushort = System.UInt16; using int = System.Int32; using uint = System.UInt32; ……
除了口无遮拦这个习惯意外,我是看不出有什么“循环定义”的。就好象说“张飞是张飞他妈生的”,这个定义怎么是循环定义了呢?给你写个demo来测试一下using System; using System.Collections.Generic;namespace ConsoleApplication1 { class Program { static void Main() { var s = new A { Name = "56" }; s.Add(new A { Name = "99" }); Console.WriteLine("s.Name={0}, s[0].Name={1}", s.Name, s[0].Name); Console.ReadKey(); } class A : List<A> { public string Name; } } }
Reflcetor也会"骗"你的眼睛的,要看这个问题还是要去到IL上看.举个例子,就明白了:有一段原始的C#代码: static void Main(string[] args) { int i = 0; Int32 j = 0; i = i + j; } 编译为dll后用Reflector查看C#源码,反射出来的是:private static void Main(string[] args) { int i = 0; int j = 0; i += j; } 发现不同没?System.Int32被反射"翻译"为int了.编译为dll后用Reflector查看VB.NET源码,反射出来的是:Private Shared Sub Main(ByVal args As String()) Dim i As Integer = 0 Dim j As Integer = 0 i = (i + j) End Sub发现有趣的地方没?System.Int32 被反射"翻译"为 Integer了. 在VB.NET中,何为Integer,其实就是C#中的 int一样,是System.Int32的别名.为什么会有这种"翻译"情况在?为什么又把C# 的int ,VB.NET中的Integer 是 System.Int32的别名呢?先不回答这个问题,再看下最初源代码生成的IL代码:.method private hidebysig static void Main(string[] args) cil managed { .entrypoint .maxstack 2 .locals init ( [0] int32 i, [1] int32 j) L_0000: ldc.i4.0 L_0001: stloc.0 L_0002: ldc.i4.0 L_0003: stloc.1 L_0004: ldloc.0 L_0005: ldloc.1 L_0006: add L_0007: stloc.0 L_0008: ret }看到奥妙之处没?IL才不管你是VB.NET的Integer,还是C#的int.你们都是我Int32的"别名"看到这里大家会纠结了.. 为什么是int32不是Int32呢?个人认为,别纠结,看下面的图片:明白了?是工具在忽悠你眼睛.
那我现在关注的是 Int32 结构中定义有如下 public const int MaxValue = 0x7fffffff; public const int MinValue = -2147483648; internal int m_value;那么,根据我的理解 int就是Int32, 那么问题来了,此定义中的int,就是Int32 那么就是在用Int32 定义 Int32 那么这个是不是就是循环定义,是不是就会有问题 此处怎么解释这就是我的疑问。
任何类型都可以这样定义using System;namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var a = new A { Name = "12" }; var b = new A { Name = "34" }; b.Name = "12"; Console.WriteLine("a.Equals(b)返回{0}", a.Equals(b)); Console.ReadKey(); } class A : IEquatable<A> { public string Name; public bool Equals(A other) { return this.Name == other.Name; } } } }假设你转不过弯来,要么是.net设计者不懂“循环定义”这个概念,要么就是你没有理解什么才叫做“循环定义”。
楼上的兄台,我不是举过例子了嘛,你是定义了一个A,然后在A内部作为参数使用A,没问题。 但是下面的代码,结构mystruct 中继续使用mystruct ,编译报错:错误 1 “consolefortest.Program.mystruct”类型的结构成员“consolefortest.Program.mystruct.m”在结构布局中导致循环 c:\users\administrator\documents\visual studio 2010\Projects\consolefortest\consolefortest\Program.cs 12 22 consolefortest不知怎么理解????求教namespace consolefortest { class Program { struct mystruct { int i; mystruct m;
代码写成这样都没问题,类中运行这样用。 class Program { static void Main(string[] args) { A myA = new A(); myA.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a = new A(); } } class A { public int i = 0; public A a; } 但是吧class A换成struct A class Program { static void Main(string[] args) {
} } struct A { public int i; public A a; }你再编译试试
编译过程中 int --> Int32 --> int注意前后2个int是不一样的 前一个int是Int32的别名 后一个是CRL里的类型而工具把后一个int解释成前一个int了 所以误导了楼主实际上,System.Runtime.InteropServices 命名空间 里有一个类 ComVisibleAttribute 控制程序集中个别托管类型、成员或所有类型对 COM 的可访问性。而Int32是这样定义的: [Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)] public struct Int32 : IComparable, IFormattable, IConvertible, IComparable<int>, IEquatable<int>注意这里有一个 ComVisible(true) 也就是说 这个Int32内部使用的int是CLR里的类型。
这是由值类型和引用类型的性质不同所决定的。 在分配内存的时候,要确定分配的内存大小,就要确定其每一个成员的内存占用。 对于 class Item 成员,仅仅需要分配一个引用的内存而已,跟实际的 class Item 的内存占用无关;而对于 struct Item 成员,这时就需要确定 struct Item 的内存占用,struct Item 又要确定其成员的内存占用... 由此,以下的代码也将是错误的: public struct StructA { public StructB b; } public struct StructB { public StructA a; }http://topic.csdn.net/t/20040719/14/3188681.html
就好像 string和String
没区别
int会根据你的CPU类型来决定长度吧。
int32直接规定了使用了32位长度。
不一样吧。
int为C#的32整形表示(32位平台)
int就是System.Int32在C#语言中的别名
没有区别,就是转换问题
bigint Int64 long
smallint int16写法不同
发现代码部分如下 public struct Int32 : IComparable, IFormattable, IConvertible, IComparable<int>, IEquatable<int>
{
public const int MaxValue = 0x7fffffff;
public const int MinValue = -2147483648;
internal int m_value;........
可以看到 int32是结构类型,而且他的定义中竟然包含了int。
如果int32=int,则出现循环定义的问题,这是不允许的。
求达人解释此现象。
System.Int32 a = 0;
int a = new int();
System.Int32 a = new System.Int32()
P102
从另一角度思考,可以想象C#编译器自动假定在所有源代码中添加了以下using指令:using sbyte = System.SByte;
using byte = System.Byte;
using short = System.Int16;
using ushort = System.UInt16;
using int = System.Int32;
using uint = System.UInt32;
……
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace AboutInt
{
class Program
{
static void Main(string[] args)
{
int a = 0;
System.Int32 b = 0;
int c = new int();
System.Int32 d = new System.Int32();
}
}
}Main方法的LI结果:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// 代码大小 10 (0xa)
.maxstack 1
.locals init ([0] int32 a,
[1] int32 b,
[2] int32 c,
[3] int32 d)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: ldc.i4.0
IL_0004: stloc.1
IL_0005: ldc.i4.0
IL_0006: stloc.2
IL_0007: ldc.i4.0
IL_0008: stloc.3
IL_0009: ret
} // end of method Program::Main
using System.Collections.Generic;namespace ConsoleApplication1
{
class Program
{
static void Main()
{
var s = new A { Name = "56" };
s.Add(new A { Name = "99" });
Console.WriteLine("s.Name={0}, s[0].Name={1}", s.Name, s[0].Name);
Console.ReadKey();
} class A : List<A>
{
public string Name;
}
}
}
extends System.ValueType
implements System.IComparable, System.IFormattable, System.IConvertible, System.IComparable`1<int32>, System.IEquatable`1<int32>
{
我指的循环定义是指一个结构的定义中引用了这个结构: struct mystruct {
int i;
mystruct m;
}
如下代码,编译会出错。
继续求解释。
http://msdn.microsoft.com/zh-cn/library/5kzh1b5w(v=VS.100).aspxInt32:
http://msdn.microsoft.com/zh-cn/library/system.int32.aspx
-2,147,483,648 到 2,147,483,647
有符号 32 位整数.NET Framework 类型 :
System.Int32
以訛傳訛之前看下MSDN,謝謝.
本人也相信int 在c#中对应的CLR类型就是Int32.但是大家都没解释一下
为啥反射了Int32结构类型,得到的源代码,继续用到了int。
这才是关键
孟子也来了,哈哈,这帖子一下子蓬荜生辉啊。不过孟子能不能解释一下,为啥反射了Int32结构类型,得到的源代码,继续用到了int。算是循环定义吗???
{
int i = 0;
Int32 j = 0;
i = i + j;
}
编译为dll后用Reflector查看C#源码,反射出来的是:private static void Main(string[] args)
{
int i = 0;
int j = 0;
i += j;
}
发现不同没?System.Int32被反射"翻译"为int了.编译为dll后用Reflector查看VB.NET源码,反射出来的是:Private Shared Sub Main(ByVal args As String())
Dim i As Integer = 0
Dim j As Integer = 0
i = (i + j)
End Sub发现有趣的地方没?System.Int32 被反射"翻译"为 Integer了.
在VB.NET中,何为Integer,其实就是C#中的 int一样,是System.Int32的别名.为什么会有这种"翻译"情况在?为什么又把C# 的int ,VB.NET中的Integer 是 System.Int32的别名呢?先不回答这个问题,再看下最初源代码生成的IL代码:.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.maxstack 2
.locals init (
[0] int32 i,
[1] int32 j)
L_0000: ldc.i4.0
L_0001: stloc.0
L_0002: ldc.i4.0
L_0003: stloc.1
L_0004: ldloc.0
L_0005: ldloc.1
L_0006: add
L_0007: stloc.0
L_0008: ret
}看到奥妙之处没?IL才不管你是VB.NET的Integer,还是C#的int.你们都是我Int32的"别名"看到这里大家会纠结了.. 为什么是int32不是Int32呢?个人认为,别纠结,看下面的图片:明白了?是工具在忽悠你眼睛.
那我现在关注的是
Int32 结构中定义有如下
public const int MaxValue = 0x7fffffff;
public const int MinValue = -2147483648;
internal int m_value;那么,根据我的理解 int就是Int32,
那么问题来了,此定义中的int,就是Int32
那么就是在用Int32 定义 Int32
那么这个是不是就是循环定义,是不是就会有问题
此处怎么解释这就是我的疑问。
{
// Methods
bool Equals(T other);
}
这个借口就是说T要是可以比较大小的,这就跟你在Int32的定义中写bool Equals(Int32 other);是一个功能,这怎么就成了“循环定义”?除了你抠字眼来先入为主地强加一个“循环定义”标签,我看不出来你还能如何解释“循环定义”这个概念。
{
class Program
{
static void Main(string[] args)
{
var a = new A { Name = "12" };
var b = new A { Name = "34" };
b.Name = "12";
Console.WriteLine("a.Equals(b)返回{0}", a.Equals(b));
Console.ReadKey();
} class A : IEquatable<A>
{
public string Name; public bool Equals(A other)
{
return this.Name == other.Name;
}
}
}
}假设你转不过弯来,要么是.net设计者不懂“循环定义”这个概念,要么就是你没有理解什么才叫做“循环定义”。
但是下面的代码,结构mystruct 中继续使用mystruct ,编译报错:错误 1 “consolefortest.Program.mystruct”类型的结构成员“consolefortest.Program.mystruct.m”在结构布局中导致循环 c:\users\administrator\documents\visual studio 2010\Projects\consolefortest\consolefortest\Program.cs 12 22 consolefortest不知怎么理解????求教namespace consolefortest
{
class Program
{
struct mystruct {
int i;
mystruct m;
}
static void Main(string[] args)
{
}
}
}
class Program
{
static void Main(string[] args)
{
A myA = new A();
myA.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a = new A();
}
}
class A
{
public int i = 0;
public A a;
}
但是吧class A换成struct A class Program
{
static void Main(string[] args)
{
}
}
struct A
{
public int i;
public A a;
}你再编译试试
ComVisibleAttribute 控制程序集中个别托管类型、成员或所有类型对 COM 的可访问性。而Int32是这样定义的:
[Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)]
public struct Int32 : IComparable, IFormattable, IConvertible, IComparable<int>, IEquatable<int>注意这里有一个 ComVisible(true) 也就是说 这个Int32内部使用的int是CLR里的类型。
}http://topic.csdn.net/t/20040719/14/3188681.html
解疑释惑了,其实一开始就怀疑这个2个int不是一样的。明白了。
可惜后面我猜错了 ComVisibleAttribute 不是干这个用的 收回
m_value : assembly int32
这个变量有个assembly修饰
static void Main(string[] args)
{
if (typeof(int) == typeof(Int32))
{
System.Console.WriteLine("Same");
}
else
{
System.Console.WriteLine("Not Same");
}
System.Console.WriteLine(typeof(int).Name);
System.Console.WriteLine(typeof(Int32).Name);
}
输出:
Same
Int32
Int32
{
A1,
A2 = 5,
A3 = 3,
A4,
A5 = -2
}
使用 public enum DemoEnum :Int32编译错误 有人知道是为什么吗?