c# - Fastest way to initialise a numeric array

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to set all elements in a numeric array to a value other than 0.
The default numeric initialisers set the elements to 0.
Is looped assignment the fastest way, or is there an equivalent to the C RTL
memset.

Cheers

Steve
 
Thanks Ron,

But I have a rather large number of elements, and I don't really want to
clutter code-space with a constant initialise.

Ciao

Steve

Ron said:
You can do this:

int[] numbers = new int[]{1,2,3,4,5,6,7,8,9};

steve said:
I want to set all elements in a numeric array to a value other than 0.
The default numeric initialisers set the elements to 0.
Is looped assignment the fastest way, or is there an equivalent to the C RTL
memset.

Cheers

Steve
 
You might have to loop through the elements. If you need to instantiate the
same array multiple times, you might want to create an instance when the app
starts and then just copy/clone the array as you need it.

steve said:
Thanks Ron,

But I have a rather large number of elements, and I don't really want to
clutter code-space with a constant initialise.

Ciao

Steve

Ron said:
You can do this:

int[] numbers = new int[]{1,2,3,4,5,6,7,8,9};

steve said:
I want to set all elements in a numeric array to a value other than 0.
The default numeric initialisers set the elements to 0.
Is looped assignment the fastest way, or is there an equivalent to the
C RTL
memset.

Cheers

Steve
 
Back
Top