According to this link, Constructors enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read.
class Program
{
static void Main(string[] args)
{
var a = new MyClass();
Console.WriteLine($"i: {a.i}, b: {a.b}"); //result --> i: 0, b: False
}
}
public class MyClass
{
public int i;
public bool b;
public MyClass() { }
}
My constructor doesn't have a single line of code. At what point the members of my class get initialized? And what if I don't provide a default constructor but rather a constructor that take some parameters? The documentation doesn't explain that assertion. Do codes similar to this get injected?
public class MyClass
{
public int i;
public bool b;
public MyClass()
{
i = default;
b = default;
}
}
Thanks for helping