In Java
instance variables can be initialized by an initialization block as shown below:
class Example {
private int varOne;
private int varTwo;
{
// Instance Initializer
varOne = 42;
varTwo = 256;
}
}
Is there an equivalent construct in C#
?
[Edit] I know that this can be inline with the instance variable declaration. However, I am looking for is something similar to the static constructor in C#
but for instance variables.
There really is no equivalent in C#. C# has only 2 ways to initialize instance varibles
There is no way to do an initialization after the object is created but before the constructor runs.
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