当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Using initializer for weakly typed assignment

问题描述:

I like the syntax:

Something something = new Something

{

Property1 = "property1",

Property2 = "property2"

}

but in the current project, I see the following:

Something something = new Something();

something["Property1"] = "property1";

something["Property2"] = "property2";

I wonder if there's a way to refactor the latter to somehow embrace the syntax of the latter. I tried to see what intellisense would suggest but didn't get any more clever. Suggestions?

网友答案:

If something is a Dictionary then

Something something = new Something()
{
    {"Property1", "property1"},
    {"Property2", "property2"}
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: