2

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.

weberc2
  • 7,423
  • 4
  • 41
  • 57

5 Answers5

13

There is a conflict with System.Double.

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

Zaid Masud
  • 13,225
  • 9
  • 67
  • 88
9

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • To address your question: firstly, I wasn't aware that System offered these structures to begin with (or, rather, that its primitives are aliases for these structures). Secondly, System.Single and System.Double are structs; I'm going for classes. Thirdly, they specify my custom class (Number) whose behavior I'd like to inherit. – weberc2 Nov 28 '12 at 20:39
2

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

nathan
  • 4,612
  • 6
  • 32
  • 33
2

This is because there is a System.Double type (its alias is double) but no System.Float: it's called System.Single - its alias is float.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

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".

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380