재우니의 블로그

C# initializing arrays

 

using System;

int[] array = new int[5];

array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
array[4] = 5;

for (int i = 0; i < array.Length; i++)
{
    Console.WriteLine(array[i]);
}

 

C# array accessing elements

 

using System;

string[] names = { "Jane", "Thomas", "Lucy", "David" };

Console.WriteLine(names[0]);
Console.WriteLine(names[1]);
Console.WriteLine(names[2]);
Console.WriteLine(names[3]);

Console.WriteLine("*************************");

Console.WriteLine(names[^1]);
Console.WriteLine(names[^2]);
Console.WriteLine(names[^3]);
Console.WriteLine(names[^4]);

 

여기서 ^1을 의미하는 부분은 array 의 끝부분 부터 접근을 하는 것으로 names[^1] 이면 제일 뒤의 첫번째인 David 값이 출력됩니다.

 

[결과물]

Jane
Thomas
Lucy
David
*************************
David
Lucy
Thomas
Jane

 

 

C# foreach array

 

using System;

int[] vals = {1, 2, 3, 4, 5};

foreach (var val in vals)
{
    Console.WriteLine(val);
}

 

C# foreach List

 

using System;
using System.Collections.Generic;

var words = new List<string> {"tea", "falcon", "book", "sky"};

foreach (var word in words)
{
    Console.WriteLine(word);
}

 

C# foreach Dictionary

 

using System;
using System.Collections.Generic;

var domains = new Dictionary<string, string>
{ 
    {"ko", "Korea"}, 
    {"ru", "Russia"},
    {"de", "Germany"},
    {"no", "Norway"}
};

foreach (var pair in domains)
{
    Console.WriteLine($"{pair.Key} - {pair.Value}");
}

Console.WriteLine("-----------------------");

foreach ((var Key, var Value) in domains) 
{
    Console.WriteLine($"{Key} - {Value}");
}

 

 

C# forEach array

 

using System;

int[] vals = {1, 2, 3, 4, 5};

Array.ForEach(vals, e => Console.WriteLine(e));

 

 

C# forEach List

 

using System;
using System.Collections.Generic;

var words = new List<string> {"tea", "falcon", "book", "sky"};

words.ForEach(e => Console.WriteLine(e));

 

 

C# foreach Dictionary

 

using System;
using System.Collections.Generic;
using System.Linq;

var domains = new Dictionary<string, string>
{ 
    {"ko", "Korea"}, 
    {"ru", "Russia"},
    {"de", "Germany"},
    {"no", "Norway"}
};

domains.ToList().ForEach(pair => Console.WriteLine($"{pair.Key} - {pair.Value}"));

 

 

참고사이트

 

https://zetcode.com/lang/csharp/arrays/

 

Arrays in C# - working with arrays in CSharp

Contents Previous Next C# arrays last modified January 9, 2021 In this part of the C# programming tutorial, we cover arrays. C# array definition An array is a collection of data. A scalar variable can hold only one item at a time. Arrays can hold multiple

zetcode.com

 

 

C# tutorial

https://zetcode.com/lang/csharp/

 

C# tutorial - beginner C# tutorial

C# tutorial last modified February 27, 2021 This is C# tutorial. In this tutorial you will learn the basics and some advanced topics of the C# language. The tutorial is suitable for beginners and intermediate programmers. Table of contents C# C# is a moder

zetcode.com

 

 

List<T>.ForEach(Action<T>) 메서드

https://docs.microsoft.com/ko-kr/dotnet/api/system.collections.generic.list-1.foreach?view=net-5.0 

 

List.ForEach(Action) 메서드 (System.Collections.Generic)

List의 각 요소에 대해 지정된 작업을 수행합니다.Performs the specified action on each element of the List.

docs.microsoft.com