First of all, let's get terminology in order. XmlReader.Create
is not a static constructor. It's just a static method that (typically) returns new instances of objects; this is normally called "factory method". A "static constructor" would be a constructor declared with keyword static
, used to initialize static members of the class:
class MyClass {
static MyClass() { ... } // static constructor
}
Now as to why a factory method may be preferable. There can be several reasons.
For one, a constructor (invoked via new
) always has to either provide a newly instantiated object, or throw an exception. A factory method can return null
if that makes sense, or it may maintain some cache of objects, and avoid creating a new one all the time (e.g. when objects are immutable).
Another reason is that when you do new T()
, you always get specifically an instance of T
. A factory method could instead create an instance of some subclass of T
, depending on input parameters and other factors. In case of XmlReader
, this is precisely what happens - XmlReader
itself is abstract
, so there cannot be any instances of it; however, there are several subclasses that serve different purposes (validating/non-validating, stream backend / DOM backed, etc), and XmlReader.Create
picks the right one based on overload and arguments you supply to it.