tags:

views:

137

answers:

4

MSDN docs state "An expression is a fragment of code that can be evaluated to a single value, object, method, or namespace."

Could someone please explain what it means for an expression to evaluate to a namespace - how can that be?

edit: fixed typo

A: 
foo = System;

System evaluates to a namespace.

(Of course, this won't compile, because you can't assign a namespace to a variable, but you get the idea.)

Matthew Scharley
You can actually do that in a using, though.
Joseph
Exactly, using statements are about the only place where you can successfully use a namespace expression. But I wanted to show an example that might fire the particular error given.
Matthew Scharley
+10  A: 

This is how the grammar is defined. Look at:

System.String

is an expression containing a dot operator that operates on a couple different expressions. System alone is considered an expression. An expression can be as simple as a single identifier or literal (hint: it's defined recursively.)

Expressions (C# 3.5 spec section §7.1: Expression classifications)

An expression is classified as one of the following:
...
A namespace. An expression with this classification can only appear as the left hand side of a member-access (§7.5.4). In any other context, an expression classified as a namespace causes a compile-time error.

Not being able to use it as, say, an argument to a method doesn't disqualify it as being considered an expression.

Mehrdad Afshari
Thanks for the education - Guess I was thinking to literal!
A: 

You can use an expression like this:

using OutlookStuff = Microsoft.Office.Outlook.This.Is.Really.Long.Isnt.It;

and then be able to use the expression later in code. This can be useful when there is namespace contention like if you have a class like MailItem or something that is common across multiple namespaces.

So you can now do this:

var mailItem = new OutlookStuff.MailItem();

EDIT I think Mehrdad's answer is the more correct answer, but I thought this was worth noting.

Joseph
wouldn't this alias have to point to a class though?using Timer=System.Windows.Forms.Timer;private readonly Timer _clock;...
NP - Thanks for posting.
Ah I got ya - The alias points to a namespace! I've only pointed them to specific types.
@paul Yeah it points the the entire namespace.
Joseph
+2  A: 

On this page on MSDN it says:

However, although a namespace name is classified as an expression, it does not evaluate to a value and therefore can never be the final result of any expression. You cannot pass a namespace name to a method parameter, or use it in a new expression, or assign it to a variable. You can only use it as a sub-expression in a larger expression. The same is true for types (as distinct from System..::.Type objects), method group names (as distinct from specific methods), and event add and remove accessors.

So in reality you can't really do anything with a namespace being a expression it always work in the same way there is nothing dynamic about it that you can influence but for the parser a namespace has to be something :). The reason it is an expression is due to the grammar of the C# language that's used to parse the code during the compilation process. It consists out of statements, expressions, operators etc... so in the case of System.Guid yourGuid = System.Guid.NewGuid() the System part would be a expression containing a namespace, the . would be an opperator and the Guid would be a type to the C# parser.

olle
The page I was on didn't have that part but I'm reading that page now - ty.
Ok, I added the link for future reference.
olle