Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 502
  • Last Modified:

Use collection initializer

On the "new" in the first line of code below, I get the warning to "Use collection initializer".
What would I do to heed this warning and would it be better than the current code?
var rpArray = new System.Collections.ArrayList();
rpArray.Add(new Microsoft.Reporting.WebForms.ReportParameter("@Number", this.Number));

Open in new window

0
allelopath
Asked:
allelopath
  • 3
  • 2
1 Solution
 
käµfm³d 👽Commented:
A connection initializer looks like this:

var rpArray = new System.Collections.ArrayList()
{
    new Microsoft.Reporting.WebForms.ReportParameter("@Number", this.Number),
    new Microsoft.Reporting.WebForms.ReportParameter("@SomeOtherNumber", this.AnotherNumber),
    new Microsoft.Reporting.WebForms.ReportParameter("@StillAnotherNumber", this.YetAnotherNumber),
};

Open in new window


I take it this warning was received from Code Analysis?
0
 
allelopathAuthor Commented:
Why is this better?
0
 
käµfm³d 👽Commented:
What tool/utility gave you the "Use collection initializer" message?
0
 
allelopathAuthor Commented:
Resharper I think
0
 
käµfm³d 👽Commented:
Honestly, I do not know, nor can I find (in searching) a reason why the analyzer would prefer the initializer. I did find that at times it can be detrimental to use an initializer. Other than that, I'm afraid I don't know what to tell you. Either approach looks fine in my opinion. Feel free to wait for others to comment on it.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now