I本人以为Int32是struct,int是整型,为啥还说这两种是一回事情呢??
类型都不一样嘛,虽然都是值类型。

解决方案 »

  1.   

    他们是一样的
    就好像  string和String
    没区别
      

  2.   

    int32是struct?
    int会根据你的CPU类型来决定长度吧。
    int32直接规定了使用了32位长度。
    不一样吧。
      

  3.   

    System.Int32为.NET Framework的32位类型表示
    int为C#的32整形表示(32位平台)
    int就是System.Int32在C#语言中的别名
    没有区别,就是转换问题
      

  4.   

    int Int32
    bigint Int64 long
    smallint int16写法不同
      

  5.   

    前几天不是有个String与string的推荐帖吗? 这帖估计也快要被推荐了。
      

  6.   

    我觉得没那么简单本人用.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,则出现循环定义的问题,这是不允许的。
    求达人解释此现象。
      

  7.   

    刚买新书《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;
    ……
      

  8.   

    所有值类型直接继承于System.ValueType。
      

  9.   

    此书我也有一本照此说法,,net reflector反射出来的int和外面使用的int还不是一种东西????
      

  10.   

    C#源码
    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
      

  11.   

    通过Reflector查看System.Int32的定义,确实存在int,不过貌似被映射到了System.Int32。
      

  12.   

    除了口无遮拦这个习惯意外,我是看不出有什么“循环定义”的。就好象说“张飞是张飞他妈生的”,这个定义怎么是循环定义了呢?给你写个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;
            }
        }
    }
      

  13.   

    另外,使用Refelctor来看源代码时,也可以用1秒钟时间切换到其它的.class public sequential ansi serializable sealed beforefieldinit Int32
        extends System.ValueType
        implements System.IComparable, System.IFormattable, System.IConvertible, System.IComparable`1<int32>, System.IEquatable`1<int32>
    {
      

  14.   


    我指的循环定义是指一个结构的定义中引用了这个结构: struct mystruct {
                int i;
                mystruct m;
            
            }
    如下代码,编译会出错。
    继续求解释。
      

  15.   

    int 在某种情况下可以等于int16、int32、int64
      

  16.   

    intEquals(Int32) == true
      

  17.   

    有CSDN就不需要MSDN了?别本末倒置了.权威的东西还是看微软自家的MSDN(当然也有错误,不过绝对比CSDN少很多很多很多)int:
    http://msdn.microsoft.com/zh-cn/library/5kzh1b5w(v=VS.100).aspxInt32:
    http://msdn.microsoft.com/zh-cn/library/system.int32.aspx
      

  18.   

    32位系统 Int32=int    Int32 是CLR 定义的,int 是c# 的类型,int 变成公共语言的时候就是Int3264位系统 int32!=int      Int32 是固定死32位, int 是根据操作系统变的。这时 int 是64位的
      

  19.   

    int
    -2,147,483,648 到 2,147,483,647
    有符号 32 位整数.NET Framework 类型 :
    System.Int32
    以訛傳訛之前看下MSDN,謝謝.
      

  20.   

    int 是 System.Int32 的简写形式
      

  21.   


    本人也相信int 在c#中对应的CLR类型就是Int32.但是大家都没解释一下
    为啥反射了Int32结构类型,得到的源代码,继续用到了int。
    这才是关键
      

  22.   


    孟子也来了,哈哈,这帖子一下子蓬荜生辉啊。不过孟子能不能解释一下,为啥反射了Int32结构类型,得到的源代码,继续用到了int。算是循环定义吗???
      

  23.   

    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呢?个人认为,别纠结,看下面的图片:明白了?是工具在忽悠你眼睛.
      

  24.   


    那我现在关注的是
    Int32 结构中定义有如下
    public const int MaxValue = 0x7fffffff;
        public const int MinValue = -2147483648;
        internal int m_value;那么,根据我的理解 int就是Int32,
    那么问题来了,此定义中的int,就是Int32
    那么就是在用Int32 定义 Int32
    那么这个是不是就是循环定义,是不是就会有问题
    此处怎么解释这就是我的疑问。
      

  25.   

    哪里看出再用Int32定义Int32了?抠字眼看出来的?public interface IEquatable<T>
    {
        // Methods
        bool Equals(T other);
    }
    这个借口就是说T要是可以比较大小的,这就跟你在Int32的定义中写bool Equals(Int32 other);是一个功能,这怎么就成了“循环定义”?除了你抠字眼来先入为主地强加一个“循环定义”标签,我看不出来你还能如何解释“循环定义”这个概念。
      

  26.   

    任何类型都可以这样定义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设计者不懂“循环定义”这个概念,要么就是你没有理解什么才叫做“循环定义”。
      

  27.   

    楼上的兄台,我不是举过例子了嘛,你是定义了一个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;
            
            }
            
            static void Main(string[] args)
            {
            }
        }
    }
      

  28.   

    Int32 在C#中的名字是int 所以两个通用 光标停在int上  按F12 看看怎么定义的不就好了  两个东西应该是一个struct  这跟平台位数无关
      

  29.   

    代码写成这样都没问题,类中运行这样用。
    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;
        }你再编译试试
      

  30.   

    编译过程中 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里的类型。
      

  31.   

    这是由值类型和引用类型的性质不同所决定的。   在分配内存的时候,要确定分配的内存大小,就要确定其每一个成员的内存占用。   对于   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 
      

  32.   


    解疑释惑了,其实一开始就怀疑这个2个int不是一样的。明白了。
      

  33.   


    可惜后面我猜错了 ComVisibleAttribute 不是干这个用的 收回
      

  34.   

    IL的int32就是System.Int32但是我看了IL的反编译代码是这样写的
    m_value : assembly int32
    这个变量有个assembly修饰
      

  35.   

    lz的几个疑问,说穿了只有CLI的规范本身能够回答,当然也涉及反编译工具的反编译原理。http://www.ecma-international.org/publications/standards/Ecma-335.htm从ECMA-335 8.2.2来看,CIL的基本类型int32和BCL中的System.Int32(当然也包含C#中的别名int)都是内建的类型,而非后期定义,所以讨论Int32的反编译结果实际意义不太大。另附Mono的Int32源代码,有一句简短的注释https://github.com/mono/mono/blob/master/mcs/class/corlib/System/Int32.cs
      

  36.   

    int 是 C# 的关键字,映射到 .net 类型 Int32,int是语言的关键字,Int32 是类型名,CLR只认识Int32,不认识int,编译器会把 int 翻译成 Int32,如果你看的反编译不是 C#的,而是 vb.net的,那么他显示的就是 Integer
      

  37.   

    其实只要拿出文档或者规范什么的文件说明,.net reflector反编译Int32类出来的int和c#中的int不是一种东西[color=#FF0000][/color],我就明白了。
      

  38.   

    reflector反编译出来的int与C#程序中的int完全是两个东西
      

  39.   

    using int = System.Int32;
      

  40.   


            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
      

  41.   

    public enum DemoEnum : int//为什么不能使用DemoEnum : Int32
        {
            A1,
            A2 = 5,
            A3 = 3,
            A4,
            A5 = -2
        }
    使用 public enum DemoEnum :Int32编译错误 有人知道是为什么吗?