Adding Values to a C# Array

Adding values to a C# array

You can do this way -

int[] terms = new int[400];
for (int runs = 0; runs < 400; runs++)
{
terms[runs] = value;
}

Alternatively, you can use Lists - the advantage with lists being, you don't need to know the array size when instantiating the list.

List<int> termsList = new List<int>();
for (int runs = 0; runs < 400; runs++)
{
termsList.Add(value);
}

// You can convert it back to an array if you would like to
int[] terms = termsList.ToArray();

Edit: a) for loops on List<T> are a bit more than 2 times cheaper than foreach loops on List<T>, b) Looping on array is around 2 times cheaper than looping on List<T>, c) looping on array using for is 5 times cheaper than looping on List<T> using foreach (which most of us do).

Best way to push into C# array

array.push is like List<T>.Add. .NET arrays are fixed-size so you can't actually add a new element. All you can do is create a new array that is one element larger than the original and then set that last element, e.g.

Array.Resize(ref myArray, myArray.Length + 1);
myArray[myArray.GetUpperBound(0)] = newValue;

EDIT:

I'm not sure that this answer actually applies given this edit to the question:

The crux of the matter is that the element needs to be added into the
first empty slot in an array, lie a Java push function would do.

The code I provided effectively appends an element. If the aim is to set the first empty element then you could do this:

int index = Array.IndexOf(myArray, null);

if (index != -1)
{
myArray[index] = newValue;
}

EDIT:

Here's an extension method that encapsulates that logic and returns the index at which the value was placed, or -1 if there was no empty element. Note that this method will work for value types too, treating an element with the default value for that type as empty.

public static class ArrayExtensions
{
public static int Push<T>(this T[] source, T value)
{
var index = Array.IndexOf(source, default(T));

if (index != -1)
{
source[index] = value;
}

return index;
}
}

How to add item to array in C#

@ShaneP,

You will need to declare an array outside of the for loop like so.

string[] releaseUriArray = new string[projectCount];

for (int intCounter = 0; intCounter < projectCount; intCounter ++)
{
var projectname = project.value[intCounter].name;
var releaseUri = "http://tfs1:8080/tfs/defaultcollection/" + projectname + "/_apis/release/releases?api-version=3.0-preview.2&searchText=911&minCreatedTime=" + date + "T00:00:00.00Z";
// Here you are adding the releaseUri strings to the releaseUriArray
releaseUriArray[intCounter] = releaseUri;

}

// print your uris from the array here
for (int intCounter = 0; intCounter < projectCount; intCounter ++)
{
var releaseUri = releaseUriArray[intCounter];
Console.WriteLine(releaseUri);
}

Add values to array using template

First you can't assign a string to an int[] array. You have to assing it to an string[] array. To solve your problem you can use the modulo % operator:

string str1 = "First String";
int String1Count = 4;

string str2 = "Second String";
int String2Count = 7;

string str3 = "Third String";
int String3Count = 5;

string[] arr1 = new string[100];

int sum12 = String1Count + String2Count;
int sum123 = String1Count + String2Count + String3Count;

for (int i = 0; i < arr1.Length; i++)
{
if (i % sum123 < String1Count)
arr1[i] = str1;
else if (i % sum123 < sum12)
arr1[i] = str2;
else if (i % sum123 < sum123)
arr1[i] = str3;
}

DEMO HERE

How to add a string to a string[] array? There's no .Add function

You can't add items to an array, since it has fixed length. What you're looking for is a List<string>, which can later be turned to an array using list.ToArray(), e.g.

List<string> list = new List<string>();
list.Add("Hi");
String[] str = list.ToArray();

How can I append values in a C# array?

Try using a List. Unlike arrays their size can be dynamically changed.

using System.Collections.Generic;

public class Example
{
public static void Main()
{
List<int> numbers = new List<int>();
numbers.add(1);
numbers.add(2);
}

}

Adding values to a string array in C#

You'll need to use a conditional loop to accomplish this, a while loop would work pretty good.

Also, using a List<string> would be better in this scenario since you (i) need to add things to it, and (ii) you don't know how big to make the array when you first declare it since you don't know how many names the user will enter.

Something like:

var names = new List<string>();
var input = Console.ReadLine();

while (input.ToUpper() != "X")
{
names.Add(input);
input = Console.ReadLine();
}


foreach (var name in names)
{
Console.WriteLine(name);
}

If you're wanting to move to the next step immediately after an user presses X (without them having to press Enter), you can look into using Console.ReadKey but it'd be more complicated since you'd have to collect one character at a time to get the name and check if they key pressed is Enter, in which case you'd move on to the next name. There's also the complexity of known when a "X" is just part of someone's name, e.g. Xavier, or whether meant to move on to the next step.

add values in string array on runtime c#

I don't get the usage of your requirement. But you can fill up the array with "0" with the following code:

List<string> list = array1.ToList();
for (int i = array1.Length; i < 7; i++)
{
list.Add("0");
}
array1 = list.ToArray();

Add value to integer array in C#

The array is not designed to be extended as new elements are added. You will need to call Array.Resize(Of T) to increase the size but this is will be quite inefficient.

Data types more in line for what you want to do is List<T>.



Related Topics



Leave a reply



Submit