First, read this: [
^] and be aware that a non-static Class defined without a constructor has a default constructor created for it. Then, read this: [
^].
Second, burn into your brain that if you define a constructor with any non-optional parameter i.e.,
any parameter that does not have its default value set in the parameter list, that a default parameterless constructor is not created.
Try this code in a Visual Studio Project:
public class MyNoConstructorClass
{
public string WhoAmI { set; get; }
}
public class MyOneParameterConstructorClass
{
public string WhoAmI { set; get; }
public MyOneParameterConstructorClass(string whoami = "nobody")
{
WhoAmI = whoami;
}
}
public class MyTwoParameterConstructorClass
{
public string WhoAmI { set; get; }
public string WhatAmI { set; get; }
public MyTwoParameterConstructorClass(string whatami, string whoami = "nobody")
{
WhatAmI = whatami;
WhoAmI = whoami;
}
}
Yes, you will get a compile error: comment out the line where compile fails, and, then:
Test it like this in some method or EventHandler: put a break-point before the first call that creates an instance of 'MyNoConstructor, and then single-step (F11 in Visual Studio) through the code observing what happens:
MyNoConstructorClass mnc = new MyNoConstructorClass();
mnc.WhoAmI = "look, ma, no constructor !";
MyOneParameterConstructorClass moc = new MyOneParameterConstructorClass();
moc.WhoAmI = "somebody with a default parameter defined";
MyTwoParameterConstructorClass m2c = new MyTwoParameterConstructorClass("one");
MyTwoParameterConstructorClass m2c2 = new MyTwoParameterConstructorClass(whatami: "weird");
MyTwoParameterConstructorClass m2c3 = new MyTwoParameterConstructorClass(whoami: "weird");
In general:
1. non-static classes will always have a 'public access point that results in an instance of the class being created ... how else would you create one if they didn't ? keep in mind that, as described above, a parameterless constructor is created by default if you haven't created one given the parameter list syntax is valid.
1.a. the circumstances in which a non-static class
requires a parameterless constructor vary.
1.b. in cases where the compiler demands you have a public parameterless constructor, you can often avoid writing one by using the 'base() directive preceded by a colon at the end of your constructor with parameters.
1.b.1. serialization is one example of when the compiler may demand a parameterless constructor. what happens, specifically, may vary with the serializer you use (XML, JSON, DataContract). I have read that some serializers can work with a parameterless constructor declared as 'private. I've never written code to test that, and don't intend to :) Marc Gravell (StackOverFlow guru) has stated that the DataContract serializer does not require a parameterless constructor.
1.b.1.a. 'dependency injection' by some external agency may also be a case where a parameterless constructor must be present.
1.b.2. in general, one might say that a parameterless public ctor on a non-static class is required when some external entity, or some usage, expects/demands it to have one.
2. static classes have an optional static constructor which cannot be declared 'public. it will be automatically invoked when any reference is made to the 'static class or its members.
3. the case of a non-static class which uses a constructor whose access-type is defined as 'private, and uses a public static variable to hold the one-and-only instance of the class is called a Singleton.
4. the initialization of 'Struct objects has its own characteristics, and you should read the documentation about that.