Loop inside list<T> initialization. #3993
-
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 18 replies
-
I'm not sure what about your idea is specific to records? Why not any type? Languages like scala allow expressions to be iterators, allowing something like: (for (i <- 1 to 250) yield new Score(i)).ToList(); which looks pretty close to what you wrote. In c# however a However you can do this: Enumerable.Range(1, 250).Select(i => new Score(i)).ToList(); Which is pretty idiomatic C#. |
Beta Was this translation helpful? Give feedback.
-
BTW instead of copying over screenshots of your code, try copying it over, and placing it between 3 backticks as follows: ```csharp This makes it easier for people to copy and paste your code, and also helps people who use screen readers :-) Thanks, and welcome to csharplang ! |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt I agree it should be to all types. public static List<Score> scores98_361 => Enumerable.Range(1, 250).Select(i => new Score(i)).ToList(); it is not the best looking but what is the performance cost from writing 250 lines of code and writing the code above ? public static List<Exam> Exams98_361 { get; set; } = new () {
new (1, new () {
new (1, "Q1/98-361-Q1-A1.jpg", true),
new (2, "Q1/98-361-Q1-A2.jpg", false),
new (3, "Q1/98-361-Q1-A3.jpg", false),
new (4, "Q1/98-361-Q1-A4.jpg", false),
}),
new (2, new () {
new (1, "Q2/98-361-Q2-A1.jpg", true),
new (2, "Q2/98-361-Q2-A2.jpg", false),
new (3, "Q2/98-361-Q2-A3.jpg", true),
new (4, "Q2/98-361-Q2-A4.jpg", false),
}),
new (3, new () {
new (1, "Q3/98-361-Q3-A1.jpg", false),
new (2, "Q3/98-361-Q3-A2.jpg", true),
new (3, "Q3/98-361-Q3-A3.jpg", false),
new (4, "Q3/98-361-Q3-A4.jpg", false),
new (5, "Q3/98-361-Q3-A5.jpg", false),
new (6, "Q3/98-361-Q3-A6.jpg", false),
}),
}; And yes I did write that 250 times in my code |
Beta Was this translation helpful? Give feedback.
-
Anything you could do in your for loop could be done with Enumerable.Range.
…On Sun, 11 Oct 2020, 14:49 Sharaf M. Mansour, ***@***.***> wrote:
@YairHalberstadt <https://github.com/YairHalberstadt> I agree it should
be to all types.
after testing your code it looked like this.
public static List<Score> scores98_361 => Enumerable.Range(1, 250).Select(i => new Score(i)).ToList();
it is not the best looking but what is the performance cost from writing
250 lines of code and writing the code above ?
Another issue will be complicated initialization Like this
public static List<Exam> Exams98_361 { get; set; } = new () {
new (1, new () {
new (1, "Q1/98-361-Q1-A1.jpg", true),
new (2, "Q1/98-361-Q1-A2.jpg", false),
new (3, "Q1/98-361-Q1-A3.jpg", false),
new (4, "Q1/98-361-Q1-A4.jpg", false),
}),
new (2, new () {
new (1, "Q2/98-361-Q2-A1.jpg", true),
new (2, "Q2/98-361-Q2-A2.jpg", false),
new (3, "Q2/98-361-Q2-A3.jpg", true),
new (4, "Q2/98-361-Q2-A4.jpg", false),
}),
new (3, new () {
new (1, "Q3/98-361-Q3-A1.jpg", false),
new (2, "Q3/98-361-Q3-A2.jpg", true),
new (3, "Q3/98-361-Q3-A3.jpg", false),
new (4, "Q3/98-361-Q3-A4.jpg", false),
new (5, "Q3/98-361-Q3-A5.jpg", false),
new (6, "Q3/98-361-Q3-A6.jpg", false),
}),
};
And yes I did write that 250 times in my code
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#3993 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADIEDQJQVDXPZRSOSPDQGOTSKGZXTANCNFSM4SLWWW2A>
.
|
Beta Was this translation helpful? Give feedback.
-
I think I'm missing why it's more complicated, but if you think it is,
that's a valid opinion.
…On Sun, 11 Oct 2020, 15:39 Sharaf M. Mansour, ***@***.***> wrote:
I know it could be done I said that it will be complicated considering I
have to loop over those 2:
public record Exam(int ID, List<Answer> Answers, string Value = "");public record Answer(int ID, string Value, bool IsCorrect, bool IsSelected = false);
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3993 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADIEDQPP3SIVSKHJU7SKOTLSKG7R5ANCNFSM4SLWWW2A>
.
|
Beta Was this translation helpful? Give feedback.
-
Or you could write it as multiple statements. There's nothing wrong with initializing a |
Beta Was this translation helpful? Give feedback.
-
Which you can still do using LINQ. And depending what you are looping
around, it could be more efficient.
…On Sun, 11 Oct 2020, 16:10 Sharaf M. Mansour, ***@***.***> wrote:
Because I will have to do a loop inside another loop.
I am just new here 😆
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3993 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADIEDQP6D7ZZ3UDK2HC7FJDSKHDHZANCNFSM4SLWWW2A>
.
|
Beta Was this translation helpful? Give feedback.
-
if you prefer a "list comprehention"-ish approach var myList = (from i in Enumerable.Range(1, 250) select new MyRecord( i )).ToList();
// or (from i in 1..251 select new MyRecord( i )).ToList(); if range to enumerable is implemeted |
Beta Was this translation helpful? Give feedback.
-
After testing I think @Flutterish approach @YairHalberstadt are the same public static List<Score> scores98_361 => Enumerable.Range(1, 250).Select(i => new Score(i)).ToList();
public static List<Score> scores98_362 => (from i in Enumerable.Range(1, 250) select new Score(i)).ToList(); I think having something like this in C# will be neat 😃 public static List<Score> scores98_361 = for(var i =1;i<251;i++) { new Score(i) };
// or
public static List<Score> scores98_361 = for(var i =1;i<251;i++) => new Score(i);
//And for each
public static List<int> Numbers = for(var i =1;i<251;i++) => i;
public static List<Score> scores98_361 = foreach (var item in Numbers) => new Score(item ); |
Beta Was this translation helpful? Give feedback.
-
Unfortunately, as we've provided a perfectly valid, currently supported, way of achieving this fairly niche-case, I don't think this is going to get much traction as a new language feature. |
Beta Was this translation helpful? Give feedback.
After testing I think @Flutterish approach @YairHalberstadt are the same
I think having something like this in C# will be neat 😃