tags:

views:

218

answers:

5
private string[] GetRoles()
{
    string[] foo = {"Test"};
    return foo;       
}

The above compiles, but...

private string[] GetRoles()
{
    return {"Test"};        
}

Does not.

Replacing it with:

return new string[] { "Test" };    

Will obviously compile though. Is this inconsistancy or am i being stupid, or am i just wrong :S.

+5  A: 

Attributed to @Luke:

The first example is just shorthand array initialization syntax. The second example is simply not legal C# syntax.

klausbyskov
Incorrect. There's no implicit casting in the first example: it's just shorthand array initialization syntax. The second example doesn't return `object[]`: it's simply not legal C# syntax.
LukeH
You are not correct, array initializators are simply not allowed in return statement.
CodeRipper
@Luke thanks for clearing that up. I did not have a compiler at hand when writing the answer. But good to see that I got a lot of upvotes for a wrong answer.
klausbyskov
+3  A: 

The array object must be created before you return as value.So

return new string[]{"Test"};

Give you the right return type.

Bug
-1 As others - the problem lies in the ability to omit operator new. You need to create an object before assigning it to a variable, there is no difference here.
CodeRipper
+1  A: 

Historically you had to write new operator. Like this:

string[] foo = new string[] {"Test"};

I think that it was in C# 2.0 that it was made possible to omit the new operator and write simply:

string[] foo = {"Test"};

where compiler can figure out that you are initializing an array. And I think I agree with you that this is an inconsistency. When the possibility to omit new operator was added they probably forgot about return statement as it's very rare to return array that was initialized inside return statement. Because when compiler looks at the return type it should be able to figure it out as well as in a case of assignment.

Personally I never omit new operator so I never even thought about it :)

CodeRipper
A: 
private string[] GetRoles()
{
    string[] foo = {"Test"};
    return foo;       
}

In the example above you are creating a new string[] array and then initialising it with one element called "Test".

private string[] GetRoles()
{
    return {"Test"};        
}

In this example you have created a method that expects to return a string array. However, you are at no point creating a new string array[]* object. Before you can add elements to an array you need to first create it. You are basically trying to returns elements from a non-existent array, which is why it fails.

You could argue the compiler could create the array for you, but it doesn't do that in your first example, so why expect it to do it in the second?

Dan Diplo
-1 In your first example you are not creating an array and then initializing. string[] foo just creates pointer to string[] and nothing more. C# spec allows you to omit operator new so the line in fact reads as string[] foo = new string[] {"Test"};. The point is why they didn't allow omitting operator new in the return statement when the type information is preset in the form of return type.
CodeRipper
In the first example the array is being initialised with one value. Are you saying "test" is not being assigned?
Dan Diplo
+2  A: 

Your snippet doesnt work for the same reason the below code does not work (as in does not compile)

private string[] GetRoles()
{
    //string[] foo = {"Test"};
    var foo = {"Test"};
    return foo;       
}

in your first snippet, the string[] gives the compiler enough information to allow the sugar to function

Allen
-1 You are showing type inference without specified type. But in the case of 'return {"Test"};' the necessary type information is the return type of the method. There is nothing preventing compiler from figuring it out.
CodeRipper