asp.net - CA1305 : int. 解析(字符串)

标签 asp.net visual-studio cultureinfo

我收到 CA1305 警告。

Microsoft.Globalization : Because the behavior of 'int.Parse(string)' could vary based on the current user's locale settings, replace this call in '_Default.CalculateImageButton_Click(object, ImageClickEventArgs)' with a call to 'int.Parse(string, IFormatProvider)'. If the result of 'int.Parse(string, IFormatProvider)' will be displayed to the user, specify 'CultureInfo.CurrentCulture' as the 'IFormatProvider' parameter. Otherwise, if the result will be stored and accessed by software, such as when it is persisted to disk or to a database, specify 'CultureInfo.InvariantCulture'.



如果在解析 Int32 时省略指定区域性,究竟会出现什么问题?

最佳答案

这意味着当您从数据文件或数据库记录中读取“1,234”,然后尝试通过 Parse 将其转换为 Int 时,您将在美国获得 1234,在德国获得 1。该警告提供了很好的建议 - 如果您正在与用户交互,请指定 CurrentCulture(感谢 Andrew!),如果您正在与文件系统或数据库(或任何用户)交互,请使用 InvariantCulture

关于asp.net - CA1305 : int. 解析(字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/984741/

相关文章:

c++ - 应用程序在 vs2013 中工作,但在用作独立 exe 时显示工件

c# - 解析后十进制不显示组(千)分隔符

jquery - ASP.NET - Bootstrap 下拉导航仅在根页面上工作

asp.net - 复制并粘贴到网络输入框会导致奇怪的行为?

windows - 如何以编程方式检测和定位 Windows 10 SDK?

visual-studio - 如何解决 Visual Studio 中的长构建问题?

不同国家的 C# 和当前本地时间

date - 无论区域设置如何,强制日期为美国格式

c# - 如何在 Web API 中支持空 RouteAttribute?

asp.net - 调用 en 外部 javascript 文件的函数