C#で配列の要素数を変更するサンプル

2021年11月19日

構文
Array.Resize(ref 配列名, 要素数)
ArrayクラスのResizeメソッドを使用して、配列の要素数を変更します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplicationSample
{
class Program
{
static void Main(string[] args)
{
string[] strLst = { "study", "skill", "in", "arkgame", "become" };
Console.WriteLine("変更前配列の要素数:");
Console.WriteLine(strLst.Length.ToString());
// 配列の要素数を変更
Array.Resize(ref strLst, 12);
Console.WriteLine("変更後配列の要素数:");
Console.WriteLine(strLst.Length.ToString());
Console.ReadKey();
}
}
}
using System; using System.Text.RegularExpressions; namespace ConsoleApplicationSample { class Program { static void Main(string[] args) { string[] strLst = { "study", "skill", "in", "arkgame", "become" }; Console.WriteLine("変更前配列の要素数:"); Console.WriteLine(strLst.Length.ToString()); // 配列の要素数を変更 Array.Resize(ref strLst, 12); Console.WriteLine("変更後配列の要素数:"); Console.WriteLine(strLst.Length.ToString()); Console.ReadKey(); } } }
using System;
using System.Text.RegularExpressions;

namespace ConsoleApplicationSample
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] strLst = { "study", "skill", "in", "arkgame", "become" };
            Console.WriteLine("変更前配列の要素数:");
            Console.WriteLine(strLst.Length.ToString());

            // 配列の要素数を変更
            Array.Resize(ref strLst, 12);
            Console.WriteLine("変更後配列の要素数:");
            Console.WriteLine(strLst.Length.ToString());

            Console.ReadKey();
        }
    }
}

実行結果

変更前配列の要素数:
5
変更後配列の要素数:
12

C#

Posted by arkgame