29 lines
505 B
Plaintext
29 lines
505 B
Plaintext
![]() |
Because the use of initializers to set properties and values makes for clearer, more communicative code, it should be preferred for initializing objects and collections.
|
||
|
|
||
|
|
||
|
== Noncompliant Code Example
|
||
|
|
||
|
----
|
||
|
var p = new Person();
|
||
|
p.Age = 5; // Noncompliant
|
||
|
p.Name = "John"; // Noncompliant
|
||
|
|
||
|
var l = new List<int>();
|
||
|
l.Add(5); // Noncompliant
|
||
|
l.Add(10); // Noncompliant
|
||
|
----
|
||
|
|
||
|
|
||
|
== Compliant Solution
|
||
|
|
||
|
----
|
||
|
var p = new Person
|
||
|
{
|
||
|
Age = 5,
|
||
|
Name = "John"
|
||
|
};
|
||
|
|
||
|
var l = new List<int> {5, 10};
|
||
|
----
|
||
|
|