BasicKnowledgeCSharp/LessonsAndTasks/Lesson 28 - Индексы и диапазоны/Program.cs
Dvurechensky 058c8f2679 1.0
Main
2024-10-05 09:59:53 +03:00

46 lines
1.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
/*
* индексы и диапазоны
*/
class Program
{
static void Main()
{
int[] myArray = { 2, 10, 5, 5, 1, 1, 2, 3 };
Console.WriteLine(myArray[^1]); // Поддерживает >= .Net Core 3.0 - выводит первый элемент с конца
int[] myIntArray = myArray[1..4]; // Поддерживает >= .Net Core 3.0 - извлекает диапазон элементов
Index index = ^2;
int res = myArray[index];
Console.WriteLine(index.Value); // Порядковый номер выводимого элемента
Console.WriteLine(index.IsFromEnd); // C начала или с конца использовать индекс
Index construct = new Index(3, true);
Console.WriteLine(construct);
Range range = new Range(2, 5);
Console.WriteLine(range);
Console.WriteLine(myArray[new Range(1, 2)]);
int[] myCR = myArray[^3..^1];
Console.WriteLine(myCR[^1]);
string str = "Hello";
Console.WriteLine(str[1..3]);
Console.WriteLine(myArray[^2]); // извлечение второго элемента с конца
Console.WriteLine();
//примеры извлечений диапазонов элементов из массива с выводом в консоль
foreach (var item in myArray[..4])
Console.WriteLine($"{item} ");
Console.WriteLine("\n");
foreach (var item in myArray[4..])
Console.WriteLine($"{item} ");
Console.WriteLine("\n");
foreach (var item in myArray[5..7])
Console.WriteLine($"{item} ");
Console.WriteLine("\n");
Console.ReadKey();
}
}