Should I use int or Int32

前端 未结 30 2622
野性不改
野性不改 2020-11-22 11:52

In C#, int and Int32 are the same thing, but I\'ve read a number of times that int is preferred over Int32 with no reason

30条回答
  •  时光说笑
    2020-11-22 12:52

    I always use the system types - e.g., Int32 instead of int. I adopted this practice after reading Applied .NET Framework Programming - author Jeffrey Richter makes a good case for using the full type names. Here are the two points that stuck with me:

    1. Type names can vary between .NET languages. For example, in C#, long maps to System.Int64 while in C++ with managed extensions, long maps to Int32. Since languages can be mixed-and-matched while using .NET, you can be sure that using the explicit class name will always be clearer, no matter the reader's preferred language.

    2. Many framework methods have type names as part of their method names:

      BinaryReader br = new BinaryReader( /* ... */ );
      float val = br.ReadSingle();     // OK, but it looks a little odd...
      Single val = br.ReadSingle();    // OK, and is easier to read
      

提交回复
热议问题