471,082 Members | 772 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,082 software developers and data experts.

Initialize Array

MAF
Is there a quick way to initialize an array with one value

for example

int[] IDs = new int[100];

I want all items to be initialized to -1.
Nov 17 '05 #1
1 6217
MAF,

You could just use a quick loop to do this:

// Initialize all elements to -1.
for (int index = 0; index < IDs.Length; ++index)
{
// Initialize.
IDs[index] = -1;
}

You might be able to get some faster performance by manipulating the
memory directly. If you want, you can use an unsafe block of code and pin
the address of the array. Once you have that, you can pass that to the
RtlFillMemory API function, specifying a value of 255 (you want this because
you want all the bits in the byte to be filled, when you have four bytes
where all the bits are filled, for an int, you have -1).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MAF" <mf*****@henwoodenergy.com> wrote in message
news:ex**************@TK2MSFTNGP10.phx.gbl...
Is there a quick way to initialize an array with one value

for example

int[] IDs = new int[100];

I want all items to be initialized to -1.

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by SamMan | last post: by
6 posts views Thread by steveneng | last post: by
4 posts views Thread by Mark Hannon | last post: by
2 posts views Thread by slack_justyb | last post: by
6 posts views Thread by Ramprasad A Padmanabhan | last post: by
2 posts views Thread by deko | last post: by
18 posts views Thread by toton | last post: by
15 posts views Thread by thinktwice | last post: by
11 posts views Thread by Bob Altman | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.