Collection Expression

noun

say it: “kuh-LEK-shun ek-SPRESH-un” /kəˈlɛkʃən ɪkˈsprɛʃən/

Square brackets around a list of values that create an array or list in one step, such as [1, 2, 3].

What is Collection Expression in C#?

Collection Expression — Square brackets around a list of values that create an array or a list in one step. The variable's type decides which one you get, and [] makes an empty one.

decimal[] prices = [12.75m, 10.25m, 8.50m];
List<string> genres = ["Mystery", "Sci-Fi"];
List<string> wishlist = [];
Console.WriteLine(prices.Length); // 3

In a sentence

  • “I filled the prices array with a collection expression: [12.75m, 10.25m].”

Level: Apprentice · More C# terms at this level

Updated