Is it able to use collection expression to initialze a OrderedDictionary?

William Liu 466 Reputation points
2024-07-26T06:48:16.44+00:00

Here is the introduction for OrderedDictionary.

My question is, is it able to write code something like below to initialize a OrderedDictionary?

OrderedDictionary orderedDictionary = [{"testKey1", "testValue1"}, {"testKey2", "testValue2"}];
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,841 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 117.2K Reputation points
    2024-07-26T07:45:28.68+00:00

    This syntax seems to work in modern C#:

    OrderedDictionary orderedDictionary = new( ) { { "testKey1", "testValue1" }, { "testKey2", "testValue2" } };
    
    // or:
    
    OrderedDictionary orderedDictionary = new( )
       {
          ["testKey1"] = "testValue1",
          ["testKey2"] = "testValue2"
       }
    

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.