public sealed class Runtime { private Runtime(); //prevents the class from being created //… }
Constructors do not do much work other than to capture the constructor parameter(s). The cost is delayed until the user uses a particular feature of the instance.
If no constructor is supplied the runtime will zero init all the fields of the structs. This makes array and static field creation faster.
There should be no difference in semantics between using the empty constructor followed by some property sets, or by using a constructor with multiple arguments. For example, the following is equivalent:
Foo foo = new Foo(); foo.A = "a"; foo.B = "b"; Foo foo = new Foo("a"); foo.B = "b"; Foo foo = new Foo("a", "b");
A common pattern for constructor parameters is to have an increasing number of parameters to let the developer specify a desired level of information. The more parameters that are specified, the more detail is specified. For all the constructors, there is a consistent order and naming of the parameters.
public class Foo { private const string defaultForA = "default value for a"; private const string defaultForB = "default value for b"; private const string defaultForC = "default value for c"; private string a; private string b; private string c; public Foo():this(defaultForA, defaultForB, defaultForC) {} public Foo(string a) : this(a, defaultForB, defaultForC) {} public Foo(string a, string b) : this(a, b, defaultForC) {} public Foo(string a, string b, string c) { /* do work here */ } }