8

When you have some property that's like:

using Algebra;

public Algebra.Vector3 Direction
{
    get { return this.direction; }
}

then compile and later change it to:

using Algebra;

public Vector3 Direction
{
    get { return this.direction; }
}

it seems like the compiled code is different between the two assemblies, which I could see using the Reflector.

Why does the compiler differentiates between the two code? Isn't it only necessary to see if there is any ambiguous type at compile time and if there isn't, have the compiled code be the same for both? I would assume the compiled code to use fully qualified names for every member at all times.

Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • 1
    Could you add the code that was created, as you see it in the reflector ? – Yochai Timmer Jan 27 '11 at 22:40
  • 1
    What *actual* differences in the compiled code have you spotted? The only possible case is `Vector3` resolves into a **different** type than `Algebra.Vector3` does. – Ondrej Tucny Jan 27 '11 at 22:41
  • 1
    Sure, it's actually shown just like the code I posted for before and after. – Joan Venge Jan 27 '11 at 22:42
  • I tried to reproduce your example. But neither reflector nor il disassembler were showing a difference ... – PetPaulsen Jan 27 '11 at 22:44
  • Is there any difference if you use ILDASM to display the metadata for the property? I don't see why there should be any difference in the metadata generated between your two code samples. – Eric Lippert Jan 27 '11 at 22:46
  • Thanks guys for replying. The problem is all I have is the 3rd party assembly I was browsing and when I viewed it from the Reflector I saw this and exported all the code. Later they released another version, and I exported the newer version too, and did a folder comparison for each file and for some files I have seen that the new code doesn't have Algebra.Vector3 and many other similar types had their namespaces removed. But certainly there are no other duplicates types with the same names because otherwise I would see them. – Joan Venge Jan 27 '11 at 23:44
  • I will see if I can show the IL for each version in the reflector. – Joan Venge Jan 27 '11 at 23:45
  • Ok I think I narrowed it down. Basically it looks the same in the Reflector window but when I select the assembly and say "Export Assembly Source Code...", it does make a difference in the exported code. Strange, but it's the exporter not the compiler. – Joan Venge Jan 27 '11 at 23:53

2 Answers2

7

I can't reproduce this. Sample code:

namespace Algebra
{
    public class Vector3 {}
}

namespace Test
{
    using Algebra;

    public class Program
    {
        private Vector3 direction = null;

        public Vector3 Direction1
        {
            get { return direction; }
        }

        public Algebra.Vector3 Direction2
        {
            get { return direction; }
        }
    }
}

The generated IL for the two properties is exactly the same, and they look the same in reflector.

My guess is that you've actually got another class called Vector3 in the "global" namespace (or some other namespace that you have a using directive for).

Try hovering over both type names in Visual Studio and see what they show.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

Here is an example that would generate the results you are observing. Hopefully this clears up what you’re asking. Assuming you have

  • a separate library containing the type Vector3 in a namespace Algebra;

  • the following files in your project:

File ①

namespace NotAlgebra
{
    public class Vector3
    {
        // ...
    }
}

File ②

using Algebra;

namespace NotAlgebra
{
    public class XYZ
    {
        // Refers to NotAlgebra.Vector3 (defined in File 1 above)
        Vector3 MyProperty1 { get; }

        // Refers to Algebra.Vector3 (defined in the external library)
        Algebra.Vector3 MyProperty2 { get; }
    }
}
Timwi
  • 65,159
  • 33
  • 165
  • 230