Instead of starting off every value at 0 like c# normally does, I am wondering if there is way to start at a specific number like -1 without having to loop through replacing every 0 with -1 after initialization?
7 Answers
using something like this
 var myArray =  Enumerable.Repeat(-1, 1000000).ToArray();
 
    
    - 19,848
- 10
- 58
- 113
- 
                    
- 
                    3Yes, it's slower mostly because `ToArray` does not know final number of elements and array is being reallocated from time to time to get more space for new elements. – MarcinJuraszek Nov 27 '13 at 01:14
- 
                    @MarcinJuraszek, i like to use ilspy to see what happen under the hood, i didn't for this case but good to know :-) – Fredou Nov 27 '13 at 01:17
Sure, possible.
void Main()
{
    var arr = Enumerable.Repeat(-1, 10).ToArray();  
    Console.WriteLine (arr);
}
Not entirely sure what happens behind the scenes so it will probably still loop over the values in your list. That will be hard to avoid though.
Different solution:
void Main()
{
    var arr = new List<int>(new int[10]).Select(x => x = -1).ToArray();
    Console.WriteLine (arr);
}
 
    
    - 43,651
- 22
- 107
- 170
You could do this:
public static int[] Initialize( this int[] instance , int value )
{
  if ( instance == null ) throw new ArgumentNullException("instance") ;
  for ( int i = 0 ; i < instance.Length ; ++i )
  {
    instance[i] = value ;
  }
  return instance ;
}
which would let you say something like
int[] foo = new int[2048].Initialize(-1) ;
You might get some performance increase by going unsafe and using pointers, since you won't incur the overhead of array bounds checking, something like this:
public static unsafe int[] Initialize( this int[] instance , int value )
{
  if ( instance == null ) throw new ArgumentNullException("instance") ;
  fixed ( int *addr = instance )
  {
    int *p    = addr ;
    int *pMax = addr+instance.Length ;
    while ( p < pMax )
    {
      *(p++) = value ;
    }
    return instance ;
  }
If you'll only ever want to set the array to -1, you can make it even faster by using memset(), since we know that all the bytes will be 0xFF. So...
    public static unsafe int[] InitializeToMinus1( this int[] instance )
    {
        if ( instance == null ) throw new ArgumentNullException("instance");
        fixed( void *p = instance )
        {
            IntPtr    addr  = new IntPtr(p) ;
            const int hexFF = 0x000000FF ;
            int       bytes = instance.Length * sizeof(int) ;
            memset( addr , hexFF , bytes ) ;
        }
        return instance ;
    }
    [DllImport("msvcrt.dll", EntryPoint="memset", CallingConvention=CallingConvention.Cdecl, SetLastError = false)]
    public static extern IntPtr memset( IntPtr addr , int c , int count ) ;
 
    
    - 71,308
- 16
- 93
- 135
- 
                    Thanks for the thorough answer! It looks like I am going to have to create something custom like this. Thanks for all the examples they were really helpful, hadent even thought of using something nonstandard to do it. – Zshelley Nov 27 '13 at 06:07
You can use Repeat:
int[] array = Enumerable.Repeat(-1, 5).ToArray();
As mentioned elsewhere, this is syntactic sugar - a loop will still be run, and you'll incur the overhead of converting the IEnumerable produced back to an array.
 
    
    - 6,595
- 3
- 34
- 48
The Enumerable class may help, like below :
IEnumerable<int> array = Enumerable.Repeat(-1, 15);  // or whatever range you'd like
 
    
    - 406
- 5
- 12
Consider the following Code...
items =  items.Select(s => s = -1).ToArray();
Good Luck!
 
    
    - 2,397
- 19
- 21
 
    