C#中Int32.Parse和Double.Parse带IFormatProvider重载默认用什么NumberStyles?
C#/.NET中两类Parse方法的默认NumberStyles实际值
Int32.Parse(string, IFormatProvider)
默认传入的NumberStyles值为NumberStyles.Integer。
这个枚举是复合值,本身已包含AllowLeadingWhite、AllowTrailingWhite、AllowLeadingSign三个子标记,和公开的接口逻辑说明一致,不存在矛盾。
Double.Parse(string, IFormatProvider)
你反汇编得到的结果准确,默认传入的是NumberStyles.Float | NumberStyles.AllowThousands。
你之前看到的长枚举组合是把复合枚举的子项全部展开后的结果:NumberStyles.Float本身就是AllowLeadingWhite | AllowTrailingWhite | AllowLeadingSign | AllowDecimalPoint | AllowExponent的组合,再加上AllowThousands之后,展开的列表和Float | AllowThousands完全等价,不存在矛盾。官方文档里提到的「通常指定的值是Float与AllowThousands的组合」,实际就是指这个默认参数的情况,属于表述模糊导致的歧义。
内容的提问来源于stack exchange,提问作者Till F.
相关产品推荐
相关产品推荐

