Re: Compiler confusion with nullable types
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Tue, 2 Dec 2008 20:54:24 -0000
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
.
- Follow-Ups:
- Re: Compiler confusion with nullable types
- From: Peter Duniho
- Re: Compiler confusion with nullable types
- References:
- Compiler confusion with nullable types
- From: Israel
- Re: Compiler confusion with nullable types
- From: Jon Skeet [C# MVP]
- Re: Compiler confusion with nullable types
- From: Peter Duniho
- Compiler confusion with nullable types
- Prev by Date: Re: Comparing two values - vs2005
- Next by Date: Why does VS 2008 pause awkwardly while editing?
- Previous by thread: Re: Compiler confusion with nullable types
- Next by thread: Re: Compiler confusion with nullable types
- Index(es):
Relevant Pages
|