Hello all,
The question is on line 3:
class Program {
static void Main(string[] args) {
//new Object[] { foo() }; If I uncomment this line, why does it not compile?
bar(new Object[] { });
bar(new Object[] { foo() });
Console.ReadKey();
}
public static object foo() {
Console.WriteLine("foo"); // A side effect
return default(object);
}
public static void bar(Object o) {
Console.WriteLine("bar");
}
}
should output:
foo
bar
foo
bar
but it does not compile. It gives the compiler error:
"Only assignment, call, increment, decrement, and new object expressions can be used as a statement"
The Answer:
new Object[] { } is(or treated as) an expression not a statement.
The Array initializers do not have any side effects.
Edit:
They are not "new object expressions".
i++ is also an expression, but there is an exemption for increment and decrement operators. (See the error message.)
This is why you can use new Object[] { } as a parameter to a function, or assign it's value to a variable, but not as a statement;
For more details, see pages 2 and 3 and the response from the C# team in blue on page 3. (Assuming default forum settings.)
Edit: (Poorly worded stuff)
Hello all,
I am trying to figure out the intricacies of the following code:
class Program {
static void Main(string[] args) {
new Object(); // This compiles
new Object[] { }; // This does not compile. Why not? Is this not syntactic sugar for array creation & initialization?
(new Object[] { }).GetType(); // This compiles.
}
}
The second line gives me:
"Only assignment, call, increment, decrement, and new object expressions can be used as a statement"
Any ideas?
After some even more confusion:
I will restate my question:
Why does the C# compiler allow statements like:
new String(); (This does throw an error because there is no no-argument constructor for String, but feel free to replace Object for String in any of these examples. My mistake. The spirit of the question is the
same. The original code at the top is correct.)
and
(new String[] { "foo", "bar"}).GetType();
and
Console.WriteLine(new String[] { "foo", "bar"});
but not:
new String[] { "foo", "bar"};
The last example gives me:
"Only assignment, call, increment, decrement, and new object expressions can be used as a statement"
I do not understand why the last statement does not compile.
To further clarify, Section 1.6 on Objects of the C# specification states:
"Instances of classes are created using the
new operator, which allocates memory for a new instance, invokes a constructor to initialize the instance, and returns a reference to the instance."
Section 1.8 on Arrays states:
"Actual array instances are created dynamically at runtime using the
new operator."
Does this imply that the new operator is overloaded?
Also, it looks like one can only declare an array if it is immediately bound to a variable, in contrast to where one can create an object even if it is not immediately bound to a variable. Ex:
new String(); and new String[] { "" }; are both not bound.
new String(); compiles and
new String[] { "" }; does not.
It appears that when using new String[] { "" } as an argument, as in
Foo(new String[] { ""}); causes
new String[] { "" } to be bound to the single argument of function
Foo and then the compiler does not complain.
If C# does indeed exhibit this strange variable binding behaviour, why is C# this way?
I am starting to think I asked at the wrong place.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.