|
var array = new[] { item };
|
|
|
|
|
Quite. ToArray and ToList are code smells. As are pretty must all Toxxx methods. (ToString can be OK, but it's often misused as well.)
|
|
|
|
|
Convert.ToString is preferred ofcourse; As .ToString can be called on a null-object, causing an exception, where Convert.ToString would return an empty string without throwing an exception.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
If the only code being consumed is your code then I agree completely but ToArray can be useful when dealing with an external library that doesn't accept List<T> . Forcing your own code to use a plain array simply to avoid a ToArray call could be much, much worse depending on how your code uses that array.
I'd lob them in with what you said about ToString . It can be useful but is often misused.
|
|
|
|
|
could be worse ...
string[] oldarray = { "A", "B", "C" };
string newitem = "D";
oldarray = (String.Join(",", oldarray) + "," + newitem).Split(',');
|
|
|
|
|
Consult your mental health professional immediately!
Cheers,
Mike Fidler
"I intend to live forever - so far, so good." Steven Wright
"I almost had a psychic girlfriend but she left me before we met." Also Steven Wright
"I'm addicted to placebos. I could quit, but it wouldn't matter." Steven Wright yet again.
|
|
|
|
|
I think a better subject would be:
How to create an array with a single item
I thought this was going to be some sort of array concatenation/insertion N^3 algorithm.
|
|
|
|
|
Exactly. Something that actually answers the original question - like this:
int[] existingArray = { 1, 12, 123, 1234 };
existingArray = existingArray.Concat(new[] { 12345 }).ToArray();
|
|
|
|
|
Adding a single item to an array causes the entire array to be reallocated and copied. Simply put, the only array that can add an item is a linked list. Some of the suggested solutions use a different datatype List<> to create a copy of the original array, use its built in function ".Add" to insert an item, then creating a brand new array containing all the items. If the array needs to change again, then a whole new copy will be created in memory. This is all okay because it works, but it's not ideal. Copying a large array over and over only to add a single item at the end repeatedly will consume more CPU cycles (electricity and time) than necessary.
Do what you have to to make it work, but then search for a better approach and adopt that when you find it.
The highest performance method would be to use a linked list. In C++, linked lists can be very fast & cheap, but there are also some implementations in C#. The List<> object is a very nice implementation of a linked list, but there is also a LinkedList<> object that is optimized for adding items anywhere within the object.
|
|
|
|
|
Since you're mentioning C++, you should be aware that the implementation of std::vector is clever enough to reallocate only occasionally, making it almost as fast as std::list for the purpose of repeated adding of elements to the end. It's only the insertion of elements in the mid or at the front that always requires a reallocation.
Of course there are always people who still manage to mess it up anyway...
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
If you think that's bad you should seen how I've seen multiple items get added to an array which the pseudo code was pretty much
Create new array that's one item bigger than the old array
Copy everything from the old array into the new one
Add one item to the slightly larger array.(The new array is now full)
repeat the previous 3 lines until you're done adding however many items you need to add.
Yes, I've literally seen that done in C++ and damn is it inefficient. (Slow and fragments memory like crazy.)
|
|
|
|
|
The solution is to avoid using arrays whenever possible, even if there is not immediate need to add items to the collection after it's been populated the first time. Even when I'm using results from the Fileinfo or DirectoryInfo objects, I store the results in a list instead of the array they return.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Encountered on r/compsci (since deleted)
Do you guys think I could fix my old Toshiba satellite from 2002 by gluing an even older Toshiba
satellite to the bottom?
Obviously I’d heat up the motherboards beforehand in a microwave to help the glue fuse the circuits.
Thoughts?
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
Why would he need glue? I'm sure he had some Duct Tape.
|
|
|
|
|
That's the least of his problems: first he has to get his orbits right to retrieve the satellites.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'm sure Laptops from 2002 were way too big to fit in a microwave.
|
|
|
|
|
Rob Grainger wrote: gluing an even older Toshiba
satellite to the bottom? sure if you want to spend a few hours in the emergency department with firefighters and nurses falling around laughing at your predicament...
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
Well, I guess it's better than cementing your head in a microwave[^] for a YouTube "prank".
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I laughed so hard the first time I saw that article. Thanks for reminding me
|
|
|
|
|
What do you get when you cross a joke with a rhetorical question?
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
Do questions with multiple question marks annoy you???
|
|
|
|
|
So long as the microwave is from the 2002 era, everything should be fine.
Outside of a dog, a book is a man's best friend; inside of a dog, it's too dark to read. -- Groucho Marx
|
|
|
|
|
I've been importing from text files, spreadsheets, etc. for over 18 years now. Just when you think you've seen it all a client brings you this:
Each line of a *.csv file starts and ends with double-quotes. Oh yes, we've had handling in place for many years to deal with embedded double-quotes in csv files (generally a sign of formatted numbers)...basically by stripping the formatting between pairs of double-quotes. It's worked great for many years until now...where it unfortunately removed all the commas leaving a big mess...not good! Of course, it's my fault!
It was an easy fix, but I'm still shaking my head over the fact that their programmer who wrote that export told me that he didn't know why it was getting formatted that way, and that it looked fine to him. (due to the fact that the double-quotes didn't show up in Excel...nevermind the fact that column A contained a comma-separated list of the items!)
I must be getting too old to argue, instantly agreed that it was my problem, fixed, tested, signed, sealed and delivered in < 30 minutes. Jpb security I guess!
"Go forth into the source" - Neal Morse
|
|
|
|
|
There is no standard for "CSV" other than the de facto standard of "whatever Excel will open".
Some of the "CSV"s I get have multiple headers and tables.
|
|
|
|
|
There is no standard for "CSV".
Yes, there is: Common Format and MIME Type for CSV Files[^].
(Admittedly, its a bit of a post facto standard)
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
That is not a standard for CSV outside of a very limited domain.
|
|
|
|