Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate/instantiate a C# array with a single value?

I know that instantiated arrays of value types in C# are automatically populated with the default value of the type (e.g. false for bool, 0 for int, etc.).

Is there a way to auto-populate an array with a seed value that's not the default? Either on creation or a built-in method afterwards (like Java's Arrays.fill())? Say I wanted an boolean array that was true by default, instead of false. Is there a built-in way to do this, or do you just have to iterate through the array with a for loop?

 // Example pseudo-code:  bool[] abValues = new[1000000];  Array.Populate(abValues, true);   // Currently how I'm handling this:  bool[] abValues = new[1000000];  for (int i = 0; i < 1000000; i++)  {      abValues[i] = true;  } 

Having to iterate through the array and "reset" each value to true seems ineffecient. Is there anyway around this? Maybe by flipping all values?

After typing this question out and thinking about it, I'm guessing that the default values are simply a result of how C# handles the memory allocation of these objects behind the scenes, so I imagine it's probably not possible to do this. But I'd still like to know for sure!

like image 424
patjbs Avatar asked Jun 18 '09 17:06

patjbs


People also ask

How do you populate an array in C#?

Using Iteration Statement to Populate an Array in C# First, we start a loop iterating through the entire array ( articles. Length ). Then, within every iteration, we assign a new Article instance to each index of the array. Note that for each iteration, we are instantiating a new article.

How do you enter initialize an array in C?

Array Initialization Using a Loop An array can also be initialized using a loop. The loop iterates from 0 to (size - 1) for accessing all indices of the array starting from 0. The following syntax uses a “for loop” to initialize the array elements. This is the most common way to initialize an array in C.

How do you instantiate an array?

To instantiate an array, use this syntax: arrayName = new datatype[ size ]; where size is an expression that evaluates to an integer and specifies the number of elements.

What are the two ways to initialize the array in C?

There are two ways to specify initializers for arrays: With C89-style initializers, array elements must be initialized in subscript order. Using designated initializers, which allow you to specify the values of the subscript elements to be initialized, array elements can be initialized in any order.

How to instantiate a class in C #?

How to instantiate a class in C#? Use the new operator to instantiate a class in C#. Let’s say our class is Line. Instantiation will create a new object as shown below − Using the object, you can now call the method −

How to use the new operator to instantiate a class?

Use the new operator to instantiate a class in C#. Let’s say our class is Line. Instantiation will create a new object as shown below −. Line line = new Line (); Using the object, you can now call the method −. line.setLength (6.0); Let us see the example −.

How do you instantiate an object using the constructor?

We new-up (instantiate) the object using the constructor we defined: One objOne = new One (); Every time we make a new instance of the "One" class, the constructor method will be run. If there is actually code in the constructor, it will be executed on instantiation of the object.

How to instantiate an object that has dynamic storage duration?

example obj3 (obj1): This line is invoking copy constructor and creates a new object obj3 that is a copy of object obj1. example *obj4 = new example (10): This is the way of instantiating an object that has dynamic storage duration.


Video Answer


1 Answers

Enumerable.Repeat(true, 1000000).ToArray(); 
like image 65
Rony Avatar answered Sep 23 '22 02:09

Rony