Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 配列の宣言と初期化 | 配列の基礎
C# 配列

配列の宣言と初期化

メニューを表示するにはスワイプしてください

Prerequisites
前提条件

配列は、C#における基本的な概念であり、同じ型の複数の値を1つの変数名で管理することができます。配列は、数値や単語のリストなど、データの集合を個別の変数を作成せずに管理したい場合に便利です。C#の配列は固定サイズで順序付けられたシーケンスとしてデータを格納し、すべての要素はゼロから始まる数値インデックスでアクセスできます。

Note
定義

配列は、同じ型の要素からなる固定サイズで順序付けられたコレクション。

Program.cs

Program.cs

12345678910111213141516171819202122232425262728293031323334
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Declare an integer array with 3 elements int[] numbers = new int[3]; // Assign values to each element numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; // Declare and initialize a string array with 2 elements string[] names = new string[2]; names[0] = "Alice"; names[1] = "Bob"; // Print the arrays Console.WriteLine("Integer array:"); Console.WriteLine(numbers[0]); Console.WriteLine(numbers[1]); Console.WriteLine(numbers[2]); Console.WriteLine("String array:"); Console.WriteLine(names[0]); Console.WriteLine(names[1]); } } }

上記のコードでは、まずサイズが3の整数型配列numbersを宣言します。これは、この配列がちょうど3つの整数を格納できることを意味します。new int[3]で配列を作成すると、各要素はその型の既定値、つまり整数の場合は0に自動的に設定されます。その後、各インデックスに値を代入します:numbers[0] = 10numbers[1] = 20numbers[2] = 30。同様に、2つの要素を持つ文字列型配列namesを宣言し、両方に値を代入します。C#の配列は常にゼロから始まるインデックスを持つため、最初の要素はインデックス0にあります。範囲外のインデックス(例えばnumbers[3])にアクセスしようとすると、実行時エラーが発生します。

Program.cs

Program.cs

123456789101112131415161718192021222324252627282930313233
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Initialize an integer array with explicit values int[] scores = { 85, 92, 78, 90 }; // Initialize a string array with explicit values string[] fruits = { "Apple", "Banana", "Cherry" }; // Print all elements and the array length Console.WriteLine("Scores array:"); for (int i = 0; i < scores.Length; i++) { Console.WriteLine(scores[i]); } Console.WriteLine("Scores array length: " + scores.Length); Console.WriteLine("Fruits array:"); for (int i = 0; i < fruits.Length; i++) { Console.WriteLine(fruits[i]); } Console.WriteLine("Fruits array length: " + fruits.Length); } } }

1. C#の配列の主な特徴は何ですか?

2. 次のうち、5つの整数の配列を宣言する正しい方法はどれですか?

question mark

C#の配列の主な特徴は何ですか?

正しい答えを選んでください

question mark

次のうち、5つの整数の配列を宣言する正しい方法はどれですか?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  1

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  1
some-alt