1

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?

Zshelley
  • 45
  • 6

7 Answers7

5

using something like this

 var myArray =  Enumerable.Repeat(-1, 1000000).ToArray();

important note; it is slower than looping manually

Fredou
  • 19,848
  • 10
  • 58
  • 113
1

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);
}
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
1

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 ) ;
Nicholas Carey
  • 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
0

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.

Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
0

Try:

int[] intArray =  Enumerable.Repeat(-1, [Length]).ToArray();
User2012384
  • 4,769
  • 16
  • 70
  • 106
0

The Enumerable class may help, like below :

IEnumerable<int> array = Enumerable.Repeat(-1, 15);  // or whatever range you'd like
0

Consider the following Code...

items =  items.Select(s => s = -1).ToArray();

Good Luck!

gpmurthy
  • 2,397
  • 19
  • 21