Re: Compiler confusion with nullable types

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Peter Duniho <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote:
On Mon, 01 Dec 2008 10:59:24 -0800, Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
wrote:

It's resolving DayOfWeek in the "DayOfWeek.Monday" to be
"this.DayOfWeek" - i.e. the property, not the type. If you rename your
property to anything else, it will be fine.

I think that the main question the OP is wondering about is why the name
conflict only causes a problem when the type of the property is
Nullable<System.DayOfWeek>, and not when the type of the property is
System.DayOfWeek.

I have to admit, I have the same confusion as the OP. It's difficult to
understand why the compiler would resolve "DayOfWeek" to the property when
the property type is nullable, but not when it's non-nullable. At the
very least, it seems like the compiler would _always_ resolve the name
"DayOfWeek" to the type, or _always_ resolve it to the property. Why does
the _type_ of the property affect how the compiler resolves the name?

I assume that if I took the time to read the C# specification carefully,
I'd find the answer. But it's not something that is readily apparent off
the top of my head.

Hmm, yes, you're right.

I'm sure you're well aware that I can't resist a little challenge like
that :)

It's section 7.5.4.1 of the C# 3.0 spec:

<quote>
In a member access of the form E.I, if E is a single identifier, and if
the meaning of E as a simple-name (§7.5.2) is a constant, field,
property, local variable, or parameter with the same type as the
meaning of E as a type-name (§3.8), then both possible meanings of E
are permitted. The two possible meanings of E.I are never ambiguous,
since I must necessarily be a member of the type E in both cases. In
other words, the rule simply permits access to the static members and
nested types of E where a compile-time error would otherwise have
occurred.
</quote>

There's then an example which pretty much mimics the code in this
question (when not using Nullable<DateTime>).

--
Jon Skeet - <skeet@xxxxxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
.



Relevant Pages

  • Re: how .net references circular references
    ... object of other class as a member. ... Can anyone explain me how .NET or its compiler will resolve this kind of reference since one class would not be compiled unless other is compiled. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: When is a Quaker a Quaker?
    ... The only meaning changed by removing the modifier is the ... name of a member of the Religious Society of Friends" is understood by ... Religious Society of Friends"), but does not change the meaning of it. ...
    (soc.religion.quaker)
  • Re: Size of a structure : Structure Padding
    ... int i1; // 4 ... According to the rules of structure padding shouldn't the size of the ... it shouldn't be 28 bytes for a 32-bit compiler. ... It may very well be that the second member of your structure, ...
    (comp.lang.c)
  • Re: Passing a form reference gives error
    ... You mentioned "implement a common interface"... ... lstFormControls is not a member of "System.Windows.Forms.Form" ... member called lstFormControls. ... compiler and shouldn't matter at runtime. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: C++ in ternms of C
    ... >A class has its member variables, ... > int b, c, d; ... >the compiler performs these checks to tell us that our code conforms to the ... >design specification of the class (for example, not changing the member ...
    (microsoft.public.vc.language)