Solved

Use collection initializer

Posted on 2013-05-23
5
459 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
  • 3
  • 2
5 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
Why is this better?
0
 
LVL 74

Expert Comment

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

Author Comment

by:allelopath
Comment Utility
Resharper I think
0
 
LVL 74

Accepted Solution

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now