468,791 Members | 1,806 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,791 developers. It's quick & easy.

anonymous arrays

This does what you'd expect ...

string[] foo = { "bar", "baz", "quux" };

.... so why is this a syntax error?

private string[] foo()
{
return { "bar", "baz", "quux" };
}

I know that it works with 'new string[]' inserted, but I don't understand
why assignment and 'return' disagree on what is a valid rvalue.

P.

--
www.CL4.org
Nov 15 '05 #1
1 4320
Paul E Collins <fi******************@CL4.org> wrote:
This does what you'd expect ...

string[] foo = { "bar", "baz", "quux" };

... so why is this a syntax error?

private string[] foo()
{
return { "bar", "baz", "quux" };
}

I know that it works with 'new string[]' inserted, but I don't understand
why assignment and 'return' disagree on what is a valid rvalue.


It's not a normal expression - it's an array initializer, which may
only be specified in field declarations, local variable declarations
and array creation expressions.

I believe this is because all of those three know exactly what type
they should be constructing beforehand, whereas for general expressions
the type of the expression is not dependent on context.

I dare say it would have been possible to include this "exception" in
other places (such as return statements), but I suspect it would have
made the specification messier. Of course, they're not really *needed*
anywhere...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Carlos Ribeiro | last post: by
3 posts views Thread by anonymous | last post: by
6 posts views Thread by Anders Thomsen | last post: by
2 posts views Thread by prakashspatil | last post: by
12 posts views Thread by =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?= | last post: by
4 posts views Thread by Frankie | last post: by
reply views Thread by Peter Duniho | last post: by
9 posts views Thread by andreyvul | last post: by
3 posts views Thread by shapper | last post: by
22 posts views Thread by Luna Moon | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.