Solved

Use collection initializer

Posted on 2013-05-23
5
493 Views
Last Modified: 2013-05-24
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
Comment
Question by:allelopath
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39191436
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
 
LVL 1

Author Comment

by:allelopath
ID: 39191481
Why is this better?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39191864
What tool/utility gave you the "Use collection initializer" message?
0
 
LVL 1

Author Comment

by:allelopath
ID: 39192159
Resharper I think
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39192726
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question