简体   繁体   中英

C# - 'Double' is an ambiguous reference

I have the following files in my code base:

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

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:

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). Why is the compiler able to distinguish between StandardUnits.Numbers.Float and float , but not between StandardUnits.Numbers.Double and double ? Why is case sensitivity not preventing this?

I can provide code snippets for Number and Float as well, if desired.

There is a conflict with System.Double .

There is no System.Float ( float is represented by System.Single instead), hence no conflict.

The CLR type for double is System.Double . That's the ambiguity.

The CLR type for float is 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. Ideally change the names so they don't conflict with types in the System namespace though... assuming you really need these types at all. (What benefits are they adding?)

This does not have anything to do with the float and double aliases in C# and case-sensitivity, really. Those are just hard-wired aliases for System.Single and System.Double respectively, and would never be ambiguous. It's your use of Double which is ambiguous.

System.Double already exists (it's built into the language.) I'm curious as to why you're creating your own version.

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.

However, float is an alias for System.Single . There is no System.Float , so there is no conflict.

See "Built-in Types" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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