I have a static class in which I use dictionaries as lookup tables to map between .NET types and SQL types. Here is an example of such a dictionary:
private static readonly Dictionary<Type, string> SqlServerMap = new Dictionary<Type, string> { {typeof (Boolean), "bit"}, {typeof (Byte[]), "varbinary(max)"}, {typeof (Double), "float"}, {typeof (Byte), "tinyint"}, {typeof (Int16), "smallint"}, {typeof (Int32), "int"}, {typeof (Int64), "bigint"}, {typeof (Decimal), "decimal"}, {typeof (Single), "real"}, {typeof (DateTime), "datetime2(7)"}, {typeof (TimeSpan), "time"}, {typeof (String), "nvarchar(MAX)"}, {typeof (Guid), "uniqueidentifier"} };
Then I have a public method that runs in a .NET type, and it returns the string value of the corresponding MS SQL Server type using this dictionary. However, since it is used as a lookup table to create database queries, I think it makes sense to do this ConcurrentDictionary. I changed it to:
private static readonly IDictionary<Type, string> SqlServerMap = new ConcurrentDictionary<Type, string> { {typeof (Boolean), "bit"}, {typeof (Byte[]), "varbinary(max)"}, {typeof (Double), "float"}, {typeof (Byte), "tinyint"}, {typeof (Int16), "smallint"}, {typeof (Int32), "int"}, {typeof (Int64), "bigint"}, {typeof (Decimal), "decimal"}, {typeof (Single), "real"}, {typeof (DateTime), "datetime2(7)"}, {typeof (TimeSpan), "time"}, {typeof (String), "nvarchar(MAX)"}, {typeof (Guid), "uniqueidentifier"} };
But now it underlines everything in red inside {} (that is, all pairs of ConcurrentDictionary key values), and the error: βUnable to access the private Add method here. I donβt think this is because I initialize it as closed static readonly because I just tested by creating a public static version and I get the same error.