Solved

Use collection initializer

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
System.Security 2 27
use of unassigned local variable 'id' 6 38
Install Problem 13 29
PrintingFoundIt(Almost!(Damn!)) 1 26
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

785 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