INDEX
Given a certain Array it is sometimes syntactically inconvenient to access its elements, the indices and ranges in c sharp 8 simplify this management. Let’s clarify the “syntactically awkward” concept with a figure:
Let’s see what indices are.
- A new struct System.Index has been introduced in the namespace System.
- A new operator has been introduced whose symbol is ^ called (index from end) that is index starting from the end.
RANGE
- A new struct System.Range has been introduced in the System namespace.
- A new operator has been introduced whose symbol is .. called (range operator) which allows us to take a slice or a slice of value.
using System; namespace IndiciRange { class Program { static void Main(string[] args) { //INDEX string[] myArray = {"primo","secondo","terzo","quarto"}; Index primo = 0; Index ultimo = ^1; string x = myArray[primo]; string y = myArray[ultimo]; Console.WriteLine($"{x} {y}"); string s1 = myArray[^1]; string s2 = myArray[^2]; Console.WriteLine($"{s1} {s2}"); //RANGE string[]a = myArray[..2]; /*- we start from the beginning of the array and we stop before the specified index 2 */ foreach(var item in a) { Console.WriteLine(item); } string[]b = myArray[2..]; /*--It starts from index two inclusive until the end*/ foreach(var item in b) { Console.WriteLine(item); } string[]c = myArray[1..3];/*--It starts from index 1 included up to index 3 excluded*/ foreach(var item in c) { Console.WriteLine(item); } } } }
INSIGHT
In C#, indexes and ranges are features introduced in C# 8.0 that allow you to work with arrays and strings in a more convenient way. Here is a detailed explanation of how they work:
Indexes
Indexes represent a position in a collection (such as an array or string). In C#, indexes can be either positive or negative:
– Positive indexes start from 0 (for the first element) to length – 1 (for the last element).
– Negative indexes start from -1 (for the last element) to -length (for the first element).
C# introduces the Index structure to represent indexes. This structure provides a convenient way to access elements from the beginning or end of the collection.
Example:
int[] array = { 1, 2, 3, 4, 5 };
Console.WriteLine(array[^1]); // Output: 5 (ultimo elemento)
Range
A range represents a subsection of a collection. In C#, ranges are represented by the Range structure, which uses two indexes to specify the beginning and end of the range.
Example:
int[] array = { 1, 2, 3, 4, 5 };
int[] subArray = array[1..3]; // Contiene gli elementi da array[1] a array[2], escluso array[3]
foreach (var item in subArray)
{
Console.WriteLine(item); // Output: 2 3
}
Syntax of Indexes and Ranges
– To create an index from the beginning of the collection: Index index = 0;
– To create an index from the end of the collection: Index index = ^1;
– To create a range that includes elements from index 1 to index 3 (excluding): Range range = 1..3;
– To create a range that includes all elements from index 1 to the end: Range range = 1..;
– To create a range that includes all elements from the beginning to index 3 (excluding): Range range = ..3;
– To create a range that includes all elements: Range range = ..;
Practical Examples
Using Indexes
string text = “Hello, World!“;
char lastChar = text[^1]; // Ottiene l’ultimo carattere ‘!’
char secondLastChar = text[^2]; // Ottiene il penultimo carattere ‘d’
Console.WriteLine(lastChar); // Output: !
Console.WriteLine(secondLastChar); // Output: d
Using Range
string text = “Hello, World!“;
string subText = text[7..12]; // Ottiene la sottostringa “World”
Console.WriteLine(subText); // Output: World
int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] subArray = numbers[3..7]; // Ottiene gli elementi {3, 4, 5, 6}
foreach (var number in subArray)
{
Console.WriteLine(number); // Output: 3 4 5 6
}
Leave A Comment