Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a ConcurrentDictionary? Error: "Cannot access private method 'Add' here"

I have a static class in which I am using 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 below which passes in a .NET type and it returns the string value of the corresponding MS SQL Server type using this dictionary. However, since this is being used as a lookup table for making database queries, I think it makes sense to make it a 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 red within the {} (i.e. all key value pairs of the ConcurrentDictionary) and the error is:

Cannot access private method 'Add' here

I don't think it's because I initialize it as private static readonly, because I just tested by making a public static version and I got the same error.

like image 351
Drew Avatar asked Aug 04 '15 17:08

Drew


People also ask

How do you initialize a concurrent dictionary?

You can provide some initial data by passing an IEnumerable<KeyValuePair<TKey, TValue>> as a parameter to the constructor, or you can call TryAdd (or AddOrUpdate , or any of the other methods with Add in the name) in a loop after creating the ConcurrentDictionary .

Is ImmutableDictionary thread safe?

If you need an immutable dictionary use ImmutableDictionary , which is just thread-safe as side-effect (if you can't modify the original instance there is no multi-threading issue).

Is ConcurrentDictionary ordered?

No. The list order of ConcurrentDictionary is NOT guaranteed, lines can come out in any order.


2 Answers

Try this

private static readonly IDictionary<Type, string> SqlServerMap =
    new ConcurrentDictionary<Type, string>(
        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"}
        }
    );

Updated: if you are using C#6(Roslyn 2.0 Compiler), you can use the new Dictionary Initializers.

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"
    };

Example https://dotnetfiddle.net/9ZgjsR

like image 91
teamchong Avatar answered Sep 28 '22 11:09

teamchong


As a code example to Servy's accepted answer, in order to initialize a ConcurrentDictionary at instantiation, you can pass a type that impements IEnumerable (like a List) of KeyValuePair types to the constructor:

private static readonly IDictionary<Type, string> SqlServerMap =
    new ConcurrentDictionary<Type, string>(
        new List<KeyValuePair<Type, string>>
        {
            new KeyValuePair<Type, string>(typeof(Boolean), "bit"),
            new KeyValuePair<Type, string>(typeof(Boolean), "bit"),
            new KeyValuePair<Type, string>(typeof(Byte[]), "varbinary(max)"),
            new KeyValuePair<Type, string>(typeof(Double), "float"),
            new KeyValuePair<Type, string>(typeof(Byte), "tinyint"),
            new KeyValuePair<Type, string>(typeof(Int16), "smallint"),
            new KeyValuePair<Type, string>(typeof(Int32), "int"),
            new KeyValuePair<Type, string>(typeof(Int64), "bigint"),
            new KeyValuePair<Type, string>(typeof(Decimal), "decimal"),
            new KeyValuePair<Type, string>(typeof(Single), "real"),
            new KeyValuePair<Type, string>(typeof(DateTime), "datetime2(7)"),
            new KeyValuePair<Type, string>(typeof(TimeSpan), "time"),
            new KeyValuePair<Type, string>(typeof(String), "nvarchar(MAX)"),
            new KeyValuePair<Type, string>(typeof(Guid), "uniqueidentifier")
        });
like image 37
Rufus L Avatar answered Sep 28 '22 10:09

Rufus L