やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

C#クイックスタート(配列とコレクション)

 Listクラスについて。

成果物

情報源

プロジェクト作成

dotnet new console -o list_tutorial

 list-tutorialとするとディレクトリ名や.csprojファイル名はそのままだが、namespacelist_tutorialに変換されてしまう。差異があると思わぬバグになりかねない。統一したいのでソリューション名に-は使わず_を使う。

配列

int nums = new int[5]() {2,4,6};
foreach (int i in nums) { Console.WriteLine($"{i}"); }

リスト

List<int> nums = new List<int>() {2,4,6};
foreach (int i in nums) { Console.WriteLine($"{i}"); }

コード

Program.cs

using System;

namespace list_tutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            new RunArray().Run();
            new RunList().Run();
        }
    }
}

RunArray.cs

using System;

namespace list_tutorial
{
    class RunArray
    {
        public void Run()
        {
            Console.WriteLine("===== RunArray.cs =====");
            code0();
            code1();
        }
        private void code0()
        {
            int[] nums = new int[3] {2,4,6};
            foreach (int i in nums) { Console.WriteLine($"{i}"); }
        }
        private void code1()
        {
            string[] strs = new string[3] {"abc", "123", "あいう"};
            foreach (string str in strs) { Console.WriteLine($"{str}"); }
        }
    }
}

RunList.cs

using System;
using System.Collections.Generic;

namespace list_tutorial
{
    class RunList
    {
        public void Run()
        {
            Console.WriteLine("===== RunList.cs =====");
            code0();
            code1();
            code2();
            code3();
            code4();
        }
        private void code0()
        {
            List<int> nums = new List<int>() {2,4,6};
            foreach (int i in nums) { Console.WriteLine($"{i}"); }
        }
        private void code1()
        {
            List<string> strs = new List<string>() {"abc", "123", "あいう"};
            foreach (string str in strs) { Console.WriteLine($"{str}"); }
        }
        private void code2()
        {
            List<string> strs = new List<string>() {"abc", "123", "あいう"};
            foreach (string str in strs) { Console.WriteLine($"{str.ToUpper()}"); } // 文字列補完
        }
        private void code3()
        {
            List<string> strs = new List<string>() {"xyz", "abc", "123", "あいう"};
            strs.Sort();
            foreach (string str in strs) { Console.WriteLine($"{str.ToUpper()}"); } // 文字列補完
        }
        private void code4()
        {
            List<string> strs = new List<string>();
            strs.Add("xyz");
            strs.Add("abc");
            strs.Add("123");
            strs.Add("だだだ");
            strs.Add("あいう");
            strs.Remove("だだだ");
            strs.Sort();
            Console.WriteLine($"Count: {strs.Count}");
            Console.WriteLine($"strs[2]: {strs[2]}");
            int index = strs.IndexOf("abc");
            Console.WriteLine($"strs[index]: {strs[index]}");
            Console.WriteLine($"strs[index]: {strs.IndexOf("abc")}");
            foreach (string str in strs) { Console.WriteLine($"{str.ToUpper()}"); }
        }
    }
}

Issue.cs

using System;
using System.Collections.Generic;

namespace list_tutorial
{
    class Issue
    {
        // フィボナッチ数列を20個まで作成せよ。
        public void Run()
        {
            Console.WriteLine("===== Issue.cs =====");
            List<int> fibo = new List<int>() {1,1};
            while (true) {
                if (20 <= fibo.Count) break;
                fibo.Add(fibo[fibo.Count-2] + fibo[fibo.Count-1]);
            }
            for (int i=0; i<fibo.Count; i++) { Console.WriteLine($"{fibo[i]}"); }
        }
    }
}

実行結果

$ dotnet run
===== RunArray.cs =====
2
4
6
abc
123
あいう
===== RunList.cs =====
2
4
6
abc
123
あいう
ABC
123
あいう
123
ABC
XYZ
あいう
Count: 4
strs[2]: xyz
strs[index]: abc
strs[index]: 1
123
ABC
XYZ
あいう
===== Issue.cs =====
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765

勉強になったこと

 $"{変数名}"みたいな書式のやつは文字列補完というらしい。

対象環境

$ uname -a
Linux raspberrypi 4.19.42-v7+ #1218 SMP Tue May 14 00:48:17 BST 2019 armv7l GNU/Linux