简体   繁体   English

C# - 'Double'是一个模棱两可的参考

[英]C# - 'Double' is an ambiguous reference

I have the following files in my code base: 我的代码库中有以下文件:

StandardUnits.Numbers.Double StandardUnits.Numbers.Double

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StandardUnits.Numbers
{
    public class Double : Number<double>
    {
        public Double(double value) : base(value) { }
        public static implicit operator Double(double value) { return new Double(value); }
        public static implicit operator double(Double value) { return value._value; }
    }
}

and

StandardUnitsTest.Program StandardUnitsTest.Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StandardUnits;
using StandardUnits.Numbers;

namespace StandardUnitsTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Equatable<string> str = "Hello! This is my number: ";
            Number<float> n = 4;
            Double o = 5.5;
            Float p = n + o;

            Console.WriteLine(str + p.ToString());
            Console.Read();
        }
    }
}

For some reason, using "Double" produces the following error: 出于某种原因,使用“Double”会产生以下错误:

Error 1 'Double' is an ambiguous reference between 'double' and 'StandardUnits.Numbers.Double' 16 13

But no such error is produced for "Float", which is made in an identical fashion to "Double" (but with "float" instead of "double", obviously). 但是没有为“Float”产生这样的错误,它以与“Double”相同的方式产生(但显然是“float”而不是“double”)。 Why is the compiler able to distinguish between StandardUnits.Numbers.Float and float , but not between StandardUnits.Numbers.Double and double ? 为什么编译器能够区分StandardUnits.Numbers.Floatfloat ,但不能区分StandardUnits.Numbers.Doubledouble Why is case sensitivity not preventing this? 为什么区分大小写不会阻止这种情况

I can provide code snippets for Number and Float as well, if desired. 如果需要,我也可以为Number和Float提供代码片段。

There is a conflict with System.Double . System.Double存在冲突。

There is no System.Float ( float is represented by System.Single instead), hence no conflict. 没有System.Float( floatSystem.Single表示),因此没有冲突。

The CLR type for double is System.Double . double的CLR类型是System.Double That's the ambiguity. 这是模棱两可的。

The CLR type for float is System.Single . float的CLR类型是System.Single That's not ambiguous with your Float type... although it does suggest that your type should be named Single instead, for consistency with the framework. 这与你的Float类型并不含糊......尽管它确实建议你的类型应该命名为Single ,以便与框架保持一致。 Ideally change the names so they don't conflict with types in the System namespace though... assuming you really need these types at all. 理想情况下,更改名称,以便它们System命名空间中的类型冲突,但是......假设您确实需要这些类型。 (What benefits are they adding?) (他们添加了哪些好处?)

This does not have anything to do with the float and double aliases in C# and case-sensitivity, really. 这与C#中的floatdouble别名没有任何关系,而且区分大小写。 Those are just hard-wired aliases for System.Single and System.Double respectively, and would never be ambiguous. 这些只是System.SingleSystem.Double硬连线别名,永远不会有歧义。 It's your use of Double which is ambiguous. 这是你对Double的使用,这是不明确的。

System.Double already exists (it's built into the language.) I'm curious as to why you're creating your own version. System.Double已经存在(它内置于语言中。)我很好奇你为什么要创建自己的版本。

http://msdn.microsoft.com/en-us/library/system.double.aspx http://msdn.microsoft.com/en-us/library/system.double.aspx

这是因为有一个System.Double类型(它的别名是double )但没有System.Float :它叫做System.Single - 它的别名是float

There's already a Double class in the System namespace ( double is an alias for it), so if you use Double , the compiler doesn't know which one you meant. System命名空间中已经有一个Double类( double是它的别名),所以如果你使用Double ,编译器就不知道你的意思。

However, float is an alias for System.Single . 但是, floatSystem.Single的别名。 There is no System.Float , so there is no conflict. 没有System.Float ,所以没有冲突。

See "Built-in Types" . 请参阅“内置类型”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM