我可以在 C# 中声明带有千位分隔符的常量整数吗?

Can I declare constant integers with a thousands separator in C#?(我可以在 C# 中声明带有千位分隔符的常量整数吗?)
本文介绍了我可以在 C# 中声明带有千位分隔符的常量整数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cobra 编程语言有一个有用的功能,您可以在数字文字中使用下划线来提高可读性.例如,以下是等价的,但第二行更容易阅读:

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 the second line is easier to read:

x = 1000000
x = 1_000_000  # obviously 1 million

C# 有什么等价的吗?

Is there anything equivalent for C#?

推荐答案

从 C# 7 开始回答

是的,这在 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;

2011 年的回答

不,C# 中没有类似的东西.你可以这样做:

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

const int x = 1000 * 1000;

但这已经是最好的了.

(请注意,此增强功能已进入 Java7 也是……也许有一天它会在 C# 中引入.)

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

这篇关于我可以在 C# 中声明带有千位分隔符的常量整数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)