List <string> candidates = new List <string> {"person1", "person2"};
第二:新しいリストを初期化中に、我々は、括弧を使用していないが、我々は、キューまたはスタックのために行うのはなぜ
Queue <int> myQueue = new Queue <int>(new int [] {0, 1, 2, 3, 4});
?リストで
List <string> candidates = new List <string> {"person1", "person2"};
第二:新しいリストを初期化中に、我々は、括弧を使用していないが、我々は、キューまたはスタックのために行うのはなぜ
Queue <int> myQueue = new Queue <int>(new int [] {0, 1, 2, 3, 4});
?リストで
、あなたは「コレクション初期化子」と呼ばれる機能を利用している、あなたはここでそれについて読むことができます: https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/classes-and-structs/object-and-collection-initializers
を基本的に、仕事に、それはコレクションIEnumerableインターフェイスを実装したに依存します「Add」メソッド(Queueにはありません)、または「Add」と呼ばれる到達可能な拡張メソッドがあります(これはあなた自身がQueueに実装できます)。
static class QueueExtensions
{
public static void Test()
{
// See that due to the extension method below, now queue can also take advantage of the collection initializer syntax.
var queue = new Queue<int> { 1, 2, 3, 5 };
}
public static void Add<T>(this Queue<T> queue, T element)
{
queue.Enqueue(element);
}
}
詳細については、こちらをご覧ください。https://stackoverflow.com/questions/8853937/why-can-i-initialize-a-list-like-an-array-in-c – Timo
最初の文ではオプションです。 'new List(){...};' 2つのステートメントには共通点がないことを確認する最も良い方法です。 –