-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
44 lines (38 loc) · 1.54 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Collections.Generic;
namespace ImmutableCollections
{
/// <summary>
/// https://devblogs.microsoft.com/dotnet/immutable-collections-ready-for-prime-time/
/// https://docs.microsoft.com/en-us/archive/msdn-magazine/2017/march/net-framework-immutable-collections
/// </summary>
class Program
{
//private static List<int> numberList = new List<int>() {1, 2, 3, 4, 5}; // reinitialize
//private static readonly List<int> numberList = new List<int>() {1, 2, 3, 4, 5}; // can't reinitialize
//private static ImmutableList<int> numberList = new List<int>() { 1, 2, 3, 4, 5 }.ToImmutableList<int>();
private static readonly IReadOnlyList<int> numberList = new[] { 1, 2, 3, 4, 5 };
//private static Dictionary<int, string> numberDictionary = new Dictionary<int, string>()
//{
// {1, "1"},
// {2, "2"},
// {3, "3"},
// {4, "4"},
// {5, "5"}
//};
private static IReadOnlyDictionary<int, string> numberDictionary = new Dictionary<int, string>()
{
{1, "1"},
{2, "2"},
{3, "3"},
{4, "4"},
{5, "5"}
};
static void Main(string[] args)
{
//numberList = new List<int>() {1, 2, 3}; // can't reinitialize, compilation error because use "readonly" keyword
//numberList[0] = -1; // IReadonlyList
//numberList.Add(10); // IReadonlyList
//numberList.Remove(2); // IReadonlyList
}
}
}