Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Return arrays in the format of initialized arrays #8419

Closed
IshamMohamed opened this issue Feb 5, 2016 · 8 comments
Closed

Proposal: Return arrays in the format of initialized arrays #8419

IshamMohamed opened this issue Feb 5, 2016 · 8 comments

Comments

@IshamMohamed
Copy link

Array initialization is a cool feature in C# meanwhile when we write a methods that returns an array, we always have to define a data (array_ inside the method, build the array with array elements and return it. But this data assignment is not always necessary if we dont use arrays. Instead of

public int AddNumbers(int number1, int number2)
{
    int sum = number1 + number2;
    return sum;
}

we can use,

public int AddNumbers(int number1, int number2)
{
    return number1+number1;
}

but in array's case we cant use this. Somehow we need to do something like the below

public string[] Method()
{
    string[] array = new string[2];
    array[0] = "THANK";
    array[1] = "YOU";
    return array;
}

My proposal is, C# should be able to support returning arrays in the format of initialized arrays,

public string[] Method()
{
    return { "THANK", "YOU" };
}

As I see this feature will be very cool and make developers bit more efficient while coding.

@tpetrina
Copy link

tpetrina commented Feb 5, 2016

You can always write:

    public string[] Method()
    {
        return new[] { "THANK", "YOU" };
    }

@bondsbw
Copy link

bondsbw commented Feb 6, 2016

Interesting. Maybe it can be used in other cases, not just return values. Could { expr, expr, ... } become shorthand for new [] { expr, expr, ... } in all cases?

public class MyClass
{
    public string[] Colors = { "Red", "Green", "Blue" };
    public int Foo()
    {
        var ints = { 1, 3, 6, 10, 15, 21 };
        return ints.Sum();
    }
}

@Richiban
Copy link

Richiban commented Feb 8, 2016

Interestingly enough the {} syntax is allowed if assigning to an explicitly typed variable. The following is legal C#:

int[] a = {1, 2, 3, 4, 5};

I'm not entirely sure why it doesn't work for a return statement.

@IshamMohamed
Copy link
Author

@bondsbw Exactly that could be another usecase.

@bondsbw
Copy link

bondsbw commented Feb 8, 2016

@Richiban Ha, I didn't even realize that was legal. So the only change my suggestion would produce is implicit typing. It would let you do things like

var x = {1, 2, 3, 4, 5}.Sum();

and

from i1 in {1, 2, 3, 4, 5}
from i2 in {10, 100, 1000}
select i1*i2

@alrz
Copy link
Contributor

alrz commented Feb 9, 2016

Perhaps this'd be addressed by #6949.

from i1 in [1, 2, 3, 4, 5]
from i2 in [10, 100, 1000]
select i1*i2

@bondsbw
Copy link

bondsbw commented Feb 9, 2016

@alrz Regardless of the outcome of that issue, the curly brace syntax is practically already in the language. The only thing that needs to be supported is implicit typing. I would prefer that happen, either way.

@gafter
Copy link
Member

gafter commented Mar 20, 2017

We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages.

@gafter gafter closed this as completed Mar 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants