【发布时间】:2018-10-18 01:11:11
【问题描述】:
我正在使用下面提到的一段代码:
IList<Comment> listComments = new List<Comment>();
foreach (var comment in paxComments.Where(x => x.Id== paxID))
{
listComments.Add(new Comment()
{
CommentID = comment.CommentId,
Text = comment.Comment,
});
}
这里的声纳说“删除这个无用的局部变量赋值”。如何在不使用新关键字初始化的情况下添加到列表中?
声纳评论是“删除这个对局部变量“listComments”的无用赋值。”
我浏览了以下链接,但没有得到答案。
Sonar complaining about useless assignment of local variable
【问题讨论】:
-
声纳注释是“删除这个对局部变量“listComments”的无用赋值。”
-
SonarQube 上安装了哪个版本的 Sonar C#?此外,如果您可以在此处发布完整的方法主体,这将有助于识别问题。
-
我没有进入 Sonar,但消息是“删除这个无用的赋值 to 局部变量 listComments”。也许它并没有假装说不需要该变量,而是说 foreach 可以以更好的方式完成。
-
@Valeri: 下面是完整的方法: private IList
GetPaxComments(IEnumerable paxCmments, int paxID) { IList listComments = new List (); foreach (var comment in paxComments.Where(x => x.Id== paxID)) { listComments.Add(new Comment() { CommentID = comment.CommentId, Text = comment.Comment, }); } 返回列表评论 } -
@AshwiniKumar 感谢您提供的方法文本。您使用的是哪个版本的 Sonar C#?我无法用最新版本和您的代码重现问题。
标签: c# sonarqube sonarqube-scan