Solved

Use collection initializer

Posted on 2013-05-23
5
480 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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