Should I use int or Int32

2018-12-31 15:43发布

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 given. Is there a reason, and should I care?

30条回答
流年柔荑漫光年
2楼-- · 2018-12-31 16:00

In my experience it's been a convention thing. I'm not aware of any technical reason to use int over Int32, but it's:

  1. Quicker to type.
  2. More familiar to the typical C# developer.
  3. A different color in the default visual studio syntax highlighting.

I'm especially fond of that last one. :)

查看更多
姐姐魅力值爆表
3楼-- · 2018-12-31 16:00

I use int in the event that Microsoft changes the default implementation for an integer to some new fangled version (let's call it Int32b).

Microsoft can then change the int alias to Int32b, and I don't have to change any of my code to take advantage of their new (and hopefully improved) integer implementation.

The same goes for any of the type keywords.

查看更多
查无此人
4楼-- · 2018-12-31 16:02

Use of Int or Int32 are the same Int is just sugar to simplify the code for the reader.

Use the Nullable variant Int? or Int32? when you work with databases on fields containing null. That will save you from a lot of runtime issues.

查看更多
人间绝色
5楼-- · 2018-12-31 16:05

You shouldn't care. You should use int most of the time. It will help the porting of your program to a wider architecture in the future (currently int is an alias to System.Int32 but that could change). Only when the bit width of the variable matters (for instance: to control the layout in memory of a struct) you should use int32 and others (with the associated "using System;").

查看更多
梦醉为红颜
6楼-- · 2018-12-31 16:07

The two are indeed synonymous; int will be a little more familiar looking, Int32 makes the 32-bitness more explicit to those reading your code. I would be inclined to use int where I just need 'an integer', Int32 where the size is important (cryptographic code, structures) so future maintainers will know it's safe to enlarge an int if appropriate, but should take care changing Int32s in the same way.

The resulting code will be identical: the difference is purely one of readability or code appearance.

查看更多
临风纵饮
7楼-- · 2018-12-31 16:08

A while back I was working on a project with Microsoft when we had a visit from someone on the Microsoft .NET CLR product team. This person coded examples and when he defined his variables he used “Int32” vs. “int” and “String” vs. “string”.

I had remembered seeing this style in other example code from Microsoft. So, I did some research and found that everyone says that there is no difference between the “Int32” and “int” except for syntax coloring. In fact, I found a lot of material suggesting you use “Int32” to make your code more readable. So, I adopted the style.

The other day I did find a difference! The compiler doesn’t allow you to type enum using the “Int32”, but it does when you use “int”. Don’t ask me why because I don’t know yet.

Example:

public  enum MyEnum : Int32
{
    AEnum = 0
}

This works.

public enum MyEnum : int
{
    AEnum = 0
}

Taken from: Int32 notation vs. int

查看更多
登录 后发表回答