Can I declare constant integers with a thousands separator in C#?

前端 未结 2 561
醉话见心
醉话见心 2021-01-07 18:50

The Cobra programming language has a useful feature where you can use underscores in numeric literals to improve readability. For example, the following are equivalent, but

相关标签:
2条回答
  • 2021-01-07 19:30

    Answer as of C# 7

    Yes, this is supported in C# 7. But be aware that there's no validation that you've put the underscores in the right place:

    // At a glance, this may look like a billion, but we accidentally missed a 0.
    int x = 1_00_000_000;
    

    Answer from 2011

    No, there's nothing like that in C#. You could do:

    const int x = 1000 * 1000;
    

    but that's about as nice as it gets.

    (Note that this enhancement went into Java 7 as well... maybe one day it will be introduced in C#.)

    0 讨论(0)
  • 2021-01-07 19:41

    Yes you can do this with C # 7.0 as shown here

    public const long BillionsAndBillions = 100_000_000_000;
    
    0 讨论(0)
提交回复
热议问题