-
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
Ideas for new features on C# 7.0 or beyond... #14549
Comments
@svick Thanks for letting me know! |
Tuple masters, is it possible to do this? (int1, int2, int3, int4, int5) = 0; "Deconstruct" a single numeric value by copying it. |
@Kalfas an issue per suggested feature is usually preferred, especially when they are unrelated. |
@dsaf No it doesn't but, with target typed default, might be possible: (int1, int2, int3, int4, int5) = default; This is equivalent to: (int1, int2, int3, int4, int5) = default((int ,int ,int ,int int)); |
@ufcpp There are no target types in your example. |
@orthoxerox Yes if int1 ~ int5 are new variables. I meant: int int1, int2, int3, int4, int5;
(int1, int2, int3, int4, int5) = default or (int int1, int int2, int int3, int int4, int int5) = default |
@ufcpp Still you have to type too much... I suggest something like the example I gave; which pretty much makes your life easier, since it combines multi-assignment and multi-declaration! |
Yes, but the syntax is a bit different: int1 = int2 = int3 = int4 = int5 = 0; |
I came up with an idea, but abuse: using System;
static class TupleExtensions
{
public static void Deconstruct<T>(this T x, out T x1, out T x2) { x1 = x; x2 = x; }
public static void Deconstruct<T>(this T x, out T x1, out T x2, out T x3) { x1 = x; x2 = x; x3 = x; }
public static void Deconstruct<T>(this T x, out T x1, out T x2, out T x3, out T x4) { x1 = x; x2 = x; x3 = x; x4 = x; }
public static void Deconstruct<T>(this T x, out T x1, out T x2, out T x3, out T x4, out T x5) { x1 = x; x2 = x; x3 = x; x4 = x; x5 = x; }
}
class Program
{
static void Main(string[] args)
{
var (a1, a2) = 2;
var (b1, b2, b3) = 'a';
var (c1, c2, c3, c4) = "abc";
var (d1, d2, d3, d4, d5) = 1.23;
Console.WriteLine(string.Join(", ", a1, a2, b1, b2, b3, c1, c2, c3, c4, d1, d2, d3, d4, d5));
}
} |
@dsaf "Maybe if majority of C# developers were writing math code most of the time..." Not necessarily, my friend. You see, this is mostly useful for declaring fields on the main classes of the applications. For instance, I may have 15 or more variables with the exact same value assigned on startup; |
@Kalfas what would be an example of such a main class? |
Is that enough? @DavidArno @dsaf |
Um, apologies if this is rude, but seriously, you actually put real-life mutable, global variables in your code? In 2016? Sorry, but I need a little lie down. I'm in shock. 😱 |
@DavidArno How else could they be accessible from the other forms as well? Plus, is there anything bad with making public variables? Lastly, the main point here is about the multi-assignment and not the fact that I use global variables in the main form so that they are accessible from other forms, too. P.S. My experience is only a year; bear with me, okay? |
@Kalfas the code is fine for one year of experience, the suggestion is fine apart for requesting a new keyword/syntax. What matters is:
#12597 ...so adding a new keyword |
How about the ability to add custom keywords or something like that then? |
Whilst it's not universally true, a good rule of thumb in programming is that if something is proving painful, then you are likely doing it wrongly. There are numerous reasons why having many public static fields in a class that all need initialising is "doing it wrongly". This relates to lots of fancy buzzwords, such as the single responsibility principle, coupling, cohesion, ease of testing, avoiding mutability, encapsulation, dependency injection, "tell, don't ask" and the like. It's beyond the scope of Roslyn issues to teach you about them. But over time, you'll learn about these topics. As you do, you'll learn better ways of programming, the size of that list of fields will decrease, and the pain will go away. Adding an |
I have a few features to suggest for the future of this language which I first learn and love.
int int1, int2, int3, int4, int5 = all 0;
bool test = 1 < 2 <= 4 > 3 >= 2 == 2 != 3 // Should return true
The operators that could be used like that are the following:
==
,>
,<
,>=
,<=
,!=
, since||
and&&
already support such a use.That's all I got for now; thanks for your time!
EDIT: As @svick said the second proposal is a duplicate, but this could maybe taken into account for multiple operators, too! (Example was even edited)
The text was updated successfully, but these errors were encountered: