1. The most common approach is collection initializer.// collection initializer syntax
// each key-value appear to be elements of set
Dictionary<int, string> sets = new Dictionary<int, string>
{
{101,"Red" },
{102,"Green" },
{103,"Blue" },
{104,"Cyan" },
{105,"Orange" },
};
2. The second approach is to use target type with new().// Target type syntax
Dictionary<int, string> sets2 = new()
{
{101,"Red" },
{102,"Green" },
{103,"Blue" },
{104,"Cyan" },
{105,"Orange" },
};
3. The third approach is to use var with new() constructor// var syntax
var sets3 = new Dictionary<int, string>()
{
{101,"Red" },
{102,"Green" },
{103,"Blue" },
{104,"Cyan" },
{105,"Orange" },
};
4. The fourth approach is to use index style where index is key// index style syntax
Dictionary<int, string> indices1 = new Dictionary<int, string>
{
[101] = "Red",
[102] = "Green",
[103] = "Blue",
[104] = "Cyan",
[105] = "Orang",
};
5. The fifth approach is to use index style with collection expression// index style syntax, collection expression
Dictionary<int, string> indices2 = new()
{
[101] = "Red",
[102] = "Green",
[103] = "Blue",
[104] = "Cyan",
[105] = "Orang",
};
6. The sixth approach is to use index style with var keyword// index style syntax, collection expression
var indices3 = new Dictionary<int, string>()
{
[101] = "Red",
[102] = "Green",
[103] = "Blue",
[104] = "Cyan",
[105] = "Orang",
};
}
}