gpt4 book ai didi

c# - 在 C# ASP.NET 中声明变量的正确或最佳方式

转载 作者:行者123 更新时间:2023-12-05 03:03:46 25 4
gpt4 key购买 nike

在以下代码中,哪个选项(A 或 B)是声明将在以下 if 语句中赋值的变量的最佳方式。

或者,是否有比 A 或 B 更好的方法。

(A) List<Dollars> amount;
(B) var amount = new List<Dollars>();

if (vortex is null)
{
amount = this.seven.Map<List<Dollars>>(twelve);
}
else
{
amount = rendered;
}

最佳答案

答案就在微软文档的帖子中 Implicitly Typed Local Variables :

The var keyword can also be useful when the specific type of the variable is tedious to type on the keyboard, or is obvious, or does not add to the readability of the code.

看线-

  • 当变量的特定类型在键盘上输入起来很乏味,或者很明显,或者确实不会增加代码的可读性。

var 以这种方式提供帮助的一个示例是嵌套泛型类型,例如用于组操作的泛型类型。在下面的查询中,查询变量的类型是 IEnumerable>。只要您和其他必须维护您的代码的人理解这一点,为了方便和简洁而使用隐式类型就没有问题。

这里的缺点是,var 的使用至少有可能使您的代码更难被其他开发人员理解。因此,C# 文档通常仅在需要时才使用 var。

你的回答是,没有。没有

best way to declare a variable

有:哪种方式最适合您的需求

关于c# - 在 C# ASP.NET 中声明变量的正确或最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53579696/

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