You need to understand What good is a builder?. Read there and you’ll practically get your answer.
Now, if you want to know why it wasn’t made out of the builder, like this:
public partial class Form1 : Form {
Farmer farmer = new Farmer { NumberOfCows = 15 };
public Form1() => InitializeComponent();
}
This is something only the author of the code can explain. At least in this case. Maybe his intention was to show that you can declare the variable in the class and can initialize it with a value in the constructor.
If so, it may be that, hypothetically, I wanted to give the right order to boot each thing. As already stated in the other question, the constructor is the only way you can control the boot order. How the variable was written farmer
starts with a null value and only after passing the constructor does it generate a value. It may be that it would be a class requirement Farmer
do this way, but nothing indicates.
It may be that it was done this way because later on it will teach to boot directly in the field farmer
in the class without passing the constructor.
It may also be that the person who wrote this code does not know that he has always been able to boot directly into the field or even into the property since C# 6. There is a lot of book that is written by those who do not master the technology. This line of books is written thinking about the reader’s cognition, I don’t know if the more technical part has the same care that they give to pedagogy.
I no longer like, for example, the fact that they teach to create a form called Form1
when maybe it should be a FarmerForm
. The book seems to encourage programming addictions.
Note that I took even the parentheses there at the initialization of the object. I talk in more detail about the subject at another question. If that’s your question, read it to understand how it works.
To tell you the truth I read this chapter of the book and found it confusing.
Understand that if the construction of the object in farmer
depend on some parameter that came from Form()
then it would make sense to initialize in the constructor, after all it is the only way to parameterize its construction. But the class Form
is not to have constructor methods with parameters. You can, but it was not created with that in mind. It may not build the way you imagine if you create a constructor with parameters. So in my opinion it was made just for the sake of.
Now, if your doubt is why you don’t:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
Farmer farmer = new Farmer { NumberOfCows = 15 };
}
}
I put in the Github for future reference.
This would be wrong in that context. This way farmer
is a local variable, so it would no longer exist at the end of the method execution. This has to do with life span and time (read there to better understand).
In this case, in theory, the variable should be used by all methods of the object created by Form1
. Again, I don’t really like the example. Because if it’s to explain the concept of creating a field I think you should use a simpler class and not involve Form
in this. If the intention is to teach use the class Form
, and that doesn’t seem to be the case, you should do it in a way that he could use.
Did any of the answers solve your question? Do you think you can accept one of them? Check out the tour how to do it, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).
– Maniero