A string array in C# is an array that stores a collection of string data type elements. However, unlike dynamic collections like List, arrays in C# are of fixed size, meaning their size is defined during declaration and cannot be altered afterward. In this C# tutorial, we will explore different ways to add values to a string array in C#.
Add Values to a String Array in C#
Now, I will show you different ways to add values to a string array in C#.
1. Using Array Initialization
The most straightforward way to populate a string array is at the time of its initialization. You can use curly braces to add elements or values to a string array in C#.
string[] names = {"Alice", "Bob", "Charlie"};
2. Using Array Index
After you declare an array, you can also add values to it using an index in C#. The index starts at 0 for the first element.
string[] names = new string[3];
names[0] = "Alice";
names[1] = "Bob";
names[2] = "Charlie";
3. Using the Array.Resize Method
While traditional arrays in C# don’t allow resizing, you can work around this by creating a new array with a different size and copying the elements over. The Array.Resize
method can do this for you.
Here’s how you can add an element to an already-declared array using Array.Resize
:
string[] names = {"Alice", "Bob"};
Array.Resize(ref names, 3);
names[2] = "Charlie";
4. Using Lists for Dynamic Sizing
If you anticipate the need to frequently resize your array, you might consider using a List<string>
instead, which can dynamically adjust its size.
List<string> namesList = new List<string> {"Alice", "Bob"};
namesList.Add("Charlie");
// Convert List to Array
string[] namesArray = namesList.ToArray();
Here is a complete example with all the methods to add values to a C# string array.
using System;
using System.Collections.Generic;
namespace StringArrayExample
{
class Program
{
static void Main(string[] args)
{
// Method 1: Initialization
string[] names = {"Emily", "Sophia", "Olivia"};
PrintArray("After Initialization", names);
// Method 2: Using index
names = new string[3];
names[0] = "Jack";
names[1] = "Mia";
names[2] = "Liam";
PrintArray("Using Array Index", names);
// Method 3: Using Array.Resize
Array.Resize(ref names, 4);
names[3] = "Noah";
PrintArray("Using Array.Resize", names);
// Method 4: Using List
List<string> namesList = new List<string> {"Isabella", "Emma", "Ava"};
namesList.Add("Sofia");
names = namesList.ToArray();
PrintArray("Using List", names);
}
static void PrintArray(string title, string[] arr)
{
Console.WriteLine($"{title}:");
foreach (string s in arr)
{
Console.Write(s + " ");
}
Console.WriteLine();
}
}
}
Once you run the code, you can see the output below:
After Initialization:
Emily Sophia Olivia
Using Array Index:
Jack Mia Liam
Using Array.Resize:
Jack Mia Liam Noah
Using List:
Isabella Emma Ava Sofia
Here is the screenshot after I ran the code using a console application.
Conclusion
While arrays in C# have a fixed size, there are multiple ways to populate them, ranging from direct initialization and using indexes to employing methods like Array.Resize
and leveraging collections like List
for dynamic resizing. I hope now you have an idea of how to add values to string array in c#.
You may also like the following tutorials:
- Single Dimensional Array in C# with Example
- create an array in asp.net using c#.net
- Check if a String Array Contains a Specific Element in C#
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…