gpt4 book ai didi

c# - 定义字典之间的区别

转载 作者:太空狗 更新时间:2023-10-29 22:06:34 24 4
gpt4 key购买 nike

我正在使用 C#(5.0).Net(4.5) 开发应用程序。我想知道下面与字典变量声明之间的区别是什么。1.

var emailtemplateValues = new Dictionary<string, object>
{
{"CreatorName", creatorUserInfo.UserName},
{"ProjectName", masterProjectInfo.Title},
{"Reason", reason}
};

和2.

var emailtemplateValues = new Dictionary<string, object>()
{
{"CreatorName", creatorUserInfo.UserName},
{"ProjectName", masterProjectInfo.Title},
{"Reason", reason}
};

在第二次声明中我使用了()Dictionary<string, object> 之后.两种语法都可以正常工作,但只是渴望了解内部工作。

最佳答案

这两种语法是等价的。当从初始化表达式中省略构造函数调用时,编译器将尝试绑定(bind)到该类型的无参数构造函数。这在 C# 规范的第 7.5.10.1 节中有介绍

An object creation expression can omit the constructor argument list and enclosing parentheses provided it includes an object initializer or collection initializer. Omitting the constructor argument list and enclosing parentheses is equivalent to specifying an empty argument list.

关于c# - 定义字典之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17939856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com