|
All options good.. except for the hard-coded '10'; really dislike hard-coding any values.
|
|
|
|
|
The 10 was just to make the example simpler
|
|
|
|
|
Understood... but I see FAR too many examples where it isn't to 'make the example simpler', rather, it's the actual code.
|
|
|
|
|
Tim Carmichael wrote: rather, it's the actual code.
Yeah, I've seen that, written that kind of stuff too. In this case, "10" is the actually some number of child nodes in an XML path, which is validated elsewhere for correctness.
|
|
|
|
|
I'm surprised that one's even legal C#. It looks like the sort of dumpsterfire you'd see in frufru bogotyped scripting languages.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason?
Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful?
--Zachris Topelius
Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies.
-- Sarah Hoyt
|
|
|
|
|
RickZeeland wrote: This seems to be about the same problem
Yeah. I should have realized the problem because I wasn't required to use a lambda. Enumerable.Repeat(()=>new Client(), 10); would be the expected syntax.
RickZeeland wrote: But I also read that using for loops is a lot faster than using Linq ...
Meh. For a service that gets called once a day and creates at most 1 to maybe 5 instances, I don't really care. The real travesty IMO is the ToArray() call because the web service interface where this gets assigned requires an array.
Marc
|
|
|
|
|
Marc Clifton wrote: Hey Marc, why are all the client names the same as the last one???
Because it does what it says on the tin!
Possibly a The Weird and The Wonderful[^] candidate?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Possibly a The Weird and The Wonderful[^] candidate?
Not firing on all cylinders today (but that piece of code was written a few weeks ago.)
|
|
|
|
|
Quote: Not firing on all cylinders today
Some days cylinders need a break, let them be cubes!
Common sense is admitting there is cause and effect and that you can exert some control over what you understand.
|
|
|
|
|
Client[] clients = new int[10].Select(i => new Client()).ToArray();
One line of code - clear and easy.
|
|
|
|
|
Nish Nishant wrote: One line of code - clear and easy.
Hah! Forgot about that version.
OT - Funny you're in Columbus OH. I had a remote gig for a couple years with a company, Emergitech (they got bought out last year, I think) that located in Columbus. Fun times!
|
|
|
|
|
Marc Clifton wrote: I had a remote gig for a couple years with a company, Emergitech (they got bought out last year, I think) that located in Columbus. Fun times!
Interesting. I don't believe I've heard of them. Their website redirects to a company that seems to be selling a crime database.
|
|
|
|
|
Nish Nishant wrote: that seems to be selling a crime database.
Well, not really a crime database, more a records management database, the idea being that the data on, say, individuals in a household, is available to everyone, from the police to the emergency responders, so people know what to expect in terms of occupants and possible past criminal records, medical conditions, prior incidents, and so forth. Of course, one of the amusing things about all this stuff was that each county (count-ee, not countr-ee) has their own database system, and of course sharing data across counties is not just a technical challenge but more often than not, a political one. Given the number of counties in a state, it gets ridiculous.
|
|
|
|
|
|
You should only need to write this once!
public static class ArrayExtensions
{
public static void Populate<T>(this T[] array, Func<int, T> createItem)
{
if (array == null) throw new ArgumentNullException(nameof(array));
if (createItem == null) throw new ArgumentNullException(nameof(createItem));
for (int index = 0; index < array.Length; index++)
{
array[index] = createItem(index);
}
}
public static T[] Create<T>(int length, Func<int, T> createItem)
{
if (length < 0) throw new ArgumentOutOfRangeException(nameof(length));
if (createItem == null) throw new ArgumentNullException(nameof(createItem));
if (length == 0) return Array.Empty<T>();
T[] result = new T[length];
Populate(result, createItem);
return result;
}
}
Usage:
Client[] clients = ArrayExtensions.Create(10, _ => new Client());
Client[] clients = new Client[10];
clients.Populate(_ => new Client());
If you're pre-4.6, you'll need to replace the Array.Empty[^] method:
public static class ArrayExtensions
{
...
public static T[] Empty<T>()
{
return EmptyArray<T>.Value;
}
private static class EmptyArray<T>
{
public static readonly T[] Value = new T[0];
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
modified 21-Sep-17 9:33am.
|
|
|
|
|
Now you made me look like I asked a programming question in the lounge.
+5!
|
|
|
|
|
I mashed the little finger on my right hand last night and tore off about half the nail.
Unfortunately, that's the finger I use for the Shift key.
Software Zen: delete this;
|
|
|
|
|
And that little piggy cried wee wee wee all the way home.
Hope the owie goes byee!
|
|
|
|
|
USE CAPS INSTEAD!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
WHAT'S THAT SONNY? I CAN'T HEAR YOU FOR ALL THE SHOUTING!
Software Zen: delete this;
|
|
|
|
|
Now you have an excuse to write everything in uppercase, use it I wish you speedy recovery
It does not solve my Problem, but it answers my question
modified 19-Jan-21 21:04pm.
|
|
|
|
|
Know anybody who's got a good C# to FORTRAN converter?
Software Zen: delete this;
|
|
|
|
|
No, but there is a guide here on Converting C# to COBOL[^] - that's all SHOUTY as well!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
ask cortana to pull up the voice input keyboard
Format Success.
Welcome to your new signa&*(gD@@@ @@@@@@*@x@@
|
|
|
|
|
At last, a sensible suggestion!
(Not very practical in the middle of a cube farm, but still...)
Software Zen: delete this;
|
|
|
|