1

I'm trying to understand the difference in variable declaration of a class inside or outside of a constructor. In this case creating the list for grades. To me, the below achieves the same result so which would be recommended and is there any reason why I should do one or the other? I have one way commented out and the other not.

class Book { //List<double> grades; List<double> grades = new List<double>(); public Book() { //Why not declare it here? //grades = new List<double>(); } public void AddGrade(double grade) { grades.Add(grade); } } 
16
  • 3
    @R.J.Dunnill Um no, that's not true at all. Commented Aug 2, 2019 at 21:36
  • 1
    @R.J.Dunnill: Looking at the commented-out lines, I think OP means why not split the declaration from the initialization. Commented Aug 2, 2019 at 21:36
  • 1
    The two methods you describe here are identical in terms of functionality. In practice, it's likely more important to be consistent rather than worry about anything else. Commented Aug 2, 2019 at 21:37
  • 1
    @R.J.Dunnill Yes, and your comment is flat out wrong. Commented Aug 2, 2019 at 21:38
  • 1
    The biggest difference is when there are multiple constructors. Initializing a class member in one place can be nice. Commented Aug 2, 2019 at 21:42

1 Answer 1

3

There's a fair bit of room for personal opinion here, but in general terms:

  1. Declaring and initializing the field on the same line helps you to verify the field is initialized without needing to look in multiple places. If you see a field declared without an initializer, you have to look in a different part of code to see whether the item is being initialized at all.
  2. There are circumstances (like when the initial value is based on a constructor argument) when you cannot initialize the field inline. Since you will sometimes need to initialize fields in the constructor, some people will argue that it's more consistent to always initialize them there.

You may also want to ask: Should I be requiring/allowing an initial value to be provided in the constructor?

 class Book { readonly List<double> grades; public Book() : this(Array.Empty<double>()) { } public Book(IEnumerable<double> grades) { this.grades = grades.ToList(); } public void AddGrade(double grade) { grades.Add(grade); } } 

And: should my class actually just be a dumb data container without a constructor or logic? (Is book.AddGrade(...) really more sensible than book.Grades.Add(...)?)

 class Book { public List<double> Grades {get;} = new List<double>(); } 
Sign up to request clarification or add additional context in comments.

5 Comments

The field will be initialized to the default value for the type if one is not specified.
One other thing to note is the order of the initialization. If I remember correctly, fields that are initialized in line (i.e., outside of the constructor) get initialized as if the code was prefixed to the constructor that is being run.
@R.J.Dunnill: Yes, technically that's true. I generally think of a null-initialized field as being effectively uninitialized.
Yes, it's not very useful.
@Flydog57 Yes, you remember correctly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.