How to properly use IReadOnlyDictionary?

后端 未结 1 526
小蘑菇
小蘑菇 2021-02-20 06:45

From msdn:

Represents a generic read-only collection of key/value pairs.

However consider following:

class Test
{
           


        
相关标签:
1条回答
  • 2021-02-20 06:53

    .NET 4.5 introduced the ReadOnlyDictionary type that you could use. It has a constructor that accepts an existing dictionary.

    When targeting lower framework versions, use the wrapper as explained in Is there a read-only generic dictionary available in .NET? and Does C# have a way of giving me an immutable Dictionary?.

    Please note that when using the latter class, the collection initializer syntax won't work; that gets compiled to Add() calls.

    0 讨论(0)
提交回复
热议问题