Add New Item in Existing Array in C#.Net

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).

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);
}

}

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 do I add just one element to an array when I call a method c#

One way would to be make your i variable global across the class:

private int arrayNum = 0;  // this is i

Then:

public void add_passenger()
{
Console.WriteLine("Enter age of passenger");
Passenger[arrayNum] = int.Parse(Console.ReadLine());
arrayNum++;
}

Note: you need to check that i < Passenger.Length before calling add_passenger()

add elements to object array

You can try

Subject[] subjects = new Subject[2];
subjects[0] = new Subject{....};
subjects[1] = new Subject{....};

alternatively you can use List

List<Subject> subjects = new List<Subject>();
subjects.add(new Subject{....});
subjects.add(new Subject{....});

how to get values from DB and add those in array in C#

You can resize the array to add a new element to it, but it's better to use List<string> in your case.

public List<string> Names= new List<string>(){ "Rick", "Morty", "John" };

and then you can add

Names.Add("Mario");

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);
}


Related Topics



Leave a reply



Submit