Code inspection: Member initialized value ignored
Category: Potential Code Quality Issues
ID: MemberInitializerValueIgnored
EditorConfig: resharper_member_initializer_value_ignored_highlighting=[error|warning|suggestion|hint|none]
Language: C#
Requires SWA: No
tip
If a constructor initializes a field from a parameter, it is not necessary to initialize the field at declaration. In the example below, JetBrains Rider removes the initialization of _contents
from its declaration because _contents
is assigned a value in the constructor.
Initializing fields within a constructor is preferable for a number of reasons. For example, if there are more than one constructor, it enables you to initialize a field with different values. Generally, in case of complex logic the initialization inside a constructor is recommended.
JetBrains Rider suggests removal of the unnecessary field initializer:
Suboptimal code
public class MyClass{ private readonly List<object> _contents = new List<object>(); public MyClass(List<object> list) { _contents = list; Console.WriteLine(_contents.ToString()); }}
After the quick-fix
public class MyClass{ private readonly List<object> _contents; public MyClass(List<object> list) { _contents = list; Console.WriteLine(_contents.ToString()); }}