When diving into the world of C#.Net programming, one of the fundamental data structures you’ll come across is the array. Arrays are immensely useful for storing and organizing a series of items sequentially. In this blog post, we’ll delve deep into the single-dimensional array in c#.net or one-dimensional array in c# with examples and understand its uses, syntax, and operations through a hands-on example.
What is a Single Dimensional Array in C#?
A single dimensional array, also referred to as a one-dimensional array, is essentially a list of items of the same type in C#. Think of it as a single row or a column in a table, where each cell in that row (or column) can store a data item. In C#, arrays are zero-indexed, meaning the first item is accessed using index 0.
Declaring a Single-Dimensional Array in C#
Declaration of a single-dimensional array in C# is straightforward. The basic syntax is as follows:
dataType[] arrayName;
For instance, to declare an integer array in C#.Net:
int[] numbers;
Initializing a Single-Dimensional Array in C#
There are several methods to initialize a one-dimensional array in C#:
- At the time of declaration:
int[] numbers = new int[5];
This creates an integer array with a size of 5, but with all elements uninitialized.
- Declaration with initialization:
int[] numbers = {1, 2, 3, 4, 5};
Here, the array is initialized with 5 integer values.
Accessing Array Elements
You can access elements in the array by referring to the index number. Remember, arrays in C# are zero-indexed.
int firstNumber = numbers[0]; // Accesses the first element of the array.
Single Dimensional Array in C# with Example
Let’s see a simple example that demonstrates the use of a single-dimensional array in C#.Net:
using System;
namespace SingleDimensionArrayExample
{
class Program
{
static void Main(string[] args)
{
// Declare and initialize the array
int[] numbers = {10, 20, 30, 40, 50};
// Print each number using a loop
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine($"Element at index {i} is: {numbers[i]}");
}
}
}
}
When you run the above program, it will display each element of the array and its corresponding index. You can check the output below:
Conclusion
In this C#.Net tutorial, we discussed, a one-dimensional array in c# with examples or a single-dimensional array in c# with examples.
You may also like:
- How to create an array in asp.net using c#.net?
- C#.Net array naming conventions with examples
- Dynamic Arrays in C# with Examples
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…