If you are working with arrays in C#, then you will definitely need to add some values in array, so in this article, I have mentioned how to add element to array c# or how to add item to array c# with console application example.
Using Basic For loop
You can use simple for
loop to add values in C# array, here is an example of using it.
using System;
using System.Linq;
namespace PushValuesInArray
{
public class Program
{
public static void Main()
{
int[] arr = new int[5];
for (int i = 0; i < arr.Length; i++)
{
//add values
arr[i] = i+1;
}
//print
Array.ForEach(arr, (e) => Console.WriteLine(e));
}
}
}
Output:
1
2
3
4
5
But this can be useful method only if your array is new, if you already have some values in your array and want to add more values, you can use Linq method or Array Resize.
Using Array.Resize
In this method, we will simply increase current Array size and set value of new array location.
using System;
using System.Linq;
namespace PushValuesInArray
{
public class Program
{
public static void ArrayPush<T>(ref T[] table, object value)
{
// Resizing the array for the cloned length (+-) (+1)
Array.Resize(ref table, table.Length + 1);
// Setting the value for the new element
table.SetValue(value, table.Length - 1);
}
public static void Main()
{
string[] fruits = { "apple", "orange" };
ArrayPush(ref fruits, "banana");
Array.ForEach(fruits, (e) => Console.WriteLine(e));
}
}
}
Output
apple
orange
banana
Using Linq
This method can be useful, if you have some values in array and want to add more values.
Here is an complete example for you.
using System;
using System.Linq;
namespace PushValuesInArray
{
public class Program
{
static void Main(string[] args)
{
// String Array with a few default values
string[] fruitsArray = new string[] { "Apple", "Banana" };
// Using the .Append() method and converting it back to a string array
fruitsArray = fruitsArray.Append("Grapes").ToArray();
// Display values
foreach (var item in fruitsArray)
{
Console.WriteLine(item);
}
}
}
}
Output:
Apple
Banana
Grapes
List to C# Array
If you are good at using List and already using it in your C# Code, then it makes very easy to convert list into C# Array using .ToArray() so you can simply use List, where possible and easily use it's .Add() method to add new elements.
Once we are done with adding items in List, simply convert them in an array.
Example:
using System;
using System.Linq;
namespace PushValuesInArray
{
public class Program
{
public static void Main()
{
// New String Array with a few default values
string[] fruitsArray = new string[] { "Apple", "Banana" };
// Convert Array To List
var fruitsList = fruitsArray.ToList();
// Add Item To List
fruitsList.Add("Orange");
// Convert back to List
fruitsArray = fruitsList.ToArray();
// Display values
foreach (var item in fruitsArray)
{
Console.WriteLine(item);
}
}
}
}
Output:
Apple
Banana
Orange
That's it, hope it solves your issue.
You may also like to read:
Resize image in C# (Console application example)
Tic Tac Toe Game in C# Console application
Clear Text File or Delete File in C#
Iterate Over Dictionary in C# (Various ways)
Using Generics in C# (With Example)
What is console application in C#? (With Example)
Useful Visual Studio Shortcuts (comment, remove comment, Collapse code etc )
How to Comment Code in Powershell