-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Comments
You can always write: public string[] Method()
{
return new[] { "THANK", "YOU" };
} |
Interesting. Maybe it can be used in other cases, not just return values. Could public class MyClass
{
public string[] Colors = { "Red", "Green", "Blue" };
public int Foo()
{
var ints = { 1, 3, 6, 10, 15, 21 };
return ints.Sum();
}
} |
Interestingly enough the {} syntax is allowed if assigning to an explicitly typed variable. The following is legal C#:
I'm not entirely sure why it doesn't work for a return statement. |
@bondsbw Exactly that could be another usecase. |
@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 |
Perhaps this'd be addressed by #6949. from i1 in [1, 2, 3, 4, 5]
from i2 in [10, 100, 1000]
select i1*i2 |
@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. |
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. |
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
we can use,
but in array's case we cant use this. Somehow we need to do something like the below
My proposal is, C# should be able to support returning arrays in the format of initialized arrays,
As I see this feature will be very cool and make developers bit more efficient while coding.
The text was updated successfully, but these errors were encountered: