Class A uses an initializer list to set the member to the paramter value, while Class B uses assignment within the constructor's body.
Can anyone give any reason to prefer one over the other as long as I'm consistent?
class A { String _filename; A(String filename) : _filename(filename) { } }
class B { String _filename; B(String filename) { _filename = filename; } }
Initialization is the process of locating and using the defined values for variable data that is used by a computer program. For example, an operating system or application program is installed with default or user-specified values that determine certain aspects of how the system or program is to function.
Static Initialization: Here, the variable is assigned a value in advance. This variable then acts as a constant. Dynamic Initialization: Here, the variable is assigned a value at the run time. The value of this variable can be altered every time the program is being run.
Initialization of Variablevariable_name=constant/literal/expression; Example: int a=10; int a=b+c; a=10; a=b+c; Multiple variables can be initialized in a single statement by single value, for example, a=b=c=d=e=10; NOTE: C variables must be declared before they are used in the c program.
In C programming language, the variables should be declared before a value is assigned to it. In an array, if fewer elements are used than the specified size of the array, then the remaining elements will be set by default to 0.
The first one is not legal in C#. The only two items that can appear after the colon in a constructor are base
and this
.
So I'd go with the second one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With