Does it really matter to distinct between short, int, long?

前端 未结 11 627
名媛妹妹
名媛妹妹 2021-01-17 09:46

In my C# app, I would like to know whether it is really important to use short for smaller numbers, int for bigger etc. Does the memory consumption really matter?

相关标签:
11条回答
  • 2021-01-17 10:41

    This is entirely relative to the amount of memory you can afford to waste. If you aren't sure, it probably doesn't matter.

    0 讨论(0)
  • 2021-01-17 10:43

    Int32 will be fine for almost anything. Exceptions include:

    • if you have specific needs where a different type is clearly better. Example: if you're writing a 16 bit emulator, Int16 (aka: short) would probably be better to represent some of the internals
    • when an API requires a certain type
    • one time, I had an invalid int cast and Visual Studio's first suggestion was to verify my value was less than infinity. I couldn't find a good type for that without using the pre-defined constants, so i used ulong since that was the closest I could come in .NET 2.0 :)
    0 讨论(0)
  • 2021-01-17 10:47

    Only you can be the judge of whether the memory consumption really matters to you. In most situations it won't make any discernible difference.

    In general, I would recommend using int/Int32 where you can get away with it. If you really need to use short, long, byte, uint etc in a particular situation then do so.

    0 讨论(0)
  • 2021-01-17 10:47

    There are a few cases where I really bother choosing.

    1. When I have memory limitations
    2. When I do bitshift operations
    3. When I care about x86/x64 portability

    Every other case is int all the way

    Edit : About x86/x64

    In x86 architecture, an int is 32 bits but in x64, an int is 64 bits

    If you write "int" everywhere and move from one architecture to another, it might leads to problems. For example you have an 32 bits api that export a long. You cast it to an integer and everything is fine. But when you move to x64, the hell breaks loose.

    The int is defined by your architecture so when you change architecture you need to be aware that it might lead to potential problems

    0 讨论(0)
  • 2021-01-17 10:49

    Memory consumption based on the type of integers you are storing is probably not an issue in a desktop or web app. In a game or a mobile device app, it may be more of an issue.

    However, the real reason to differentiate between the types is the kind of numbers you need to store. If you have really big numbers, or high precision, you may need to use long to store it.

    0 讨论(0)
提交回复
热议问题