Set Literal
Summary
Creates a set or unordered set and, optionally, populates it with values.
Syntax
{ expression1, expression2, ..., expressionN }
Parameters
- expressionN
- Any valid expression.
Description
The set literal notation is used for creating instances of sets or unordered sets. Sets are collections of unique values; no values are repeated in sets. The set literal notation can be used as a shorthand for instantiating System.OrderedSet or System.UnorderedSet.
A common use case for sets is checking for the existence of value(s) within a set. Unordered sets guarantee an O(1) constant time complexity for lookups but lack information about element sort order. In unordered sets, the value is also its key, which uniquely identifies the set element. Internally, unordered sets are implemented using a hash algorithm.
When it is desirable for information on sort order to be retained, ordered sets can be used. Ordered sets have a worst-case time complexity of O(m log n) for lookups, where m and n are the number of elements in each tree.
Examples
1 2 3 4 5 6 7 8 9 10 11 12 | import System; OrderedSet< string > employeeNames = { "Peter" , "John" , "Joseph" }; foreach ( string name in employees) { Console.log(name); } // Output: // Peter // John // Joseph |
See Also
Share
HTML | BBCode | Direct Link