I have an array of data that gets zeroed out from time to time. To do that, should I instantiate a new array, or use the Array.Clear method?
For instance,
int workingSet = new int[5000];
// Other code here
workingSet = new int[5000];
// or
Array.Clear(workingSet, 0, 5000);
Clear(Array)Clears the contents of an array.
The array is faster in case of access to an element while List is faster in case of adding/deleting an element from the collection.
Arrays can store data very compactly and are more efficient for storing large amounts of data. Arrays are great for numerical operations; lists cannot directly handle math operations. For example, you can divide each element of an array by the same number with just one line of code.
An array is faster than a list in python since all the elements stored in an array are homogeneous i.e., they have the same data type whereas a list contains heterogeneous elements.
When you make a new array instead of an old one, C# will:
When you keep an old array, C# will
Everything else being equal, the second approach is more efficient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With