What is the difference between static, internal and public constructors?

Shankaranarayana picture Shankaranarayana · Aug 10, 2011 · Viewed 25.3k times · Source

What is the difference between static, internal and public constructors? Why do we need to create all of them together?

 static xyz()
 {
 }

 public xyz()
 {
 }

 internal xyz()
 {
 }

Answer

Rune FS picture Rune FS · Aug 10, 2011

The static constructor will be called the first time an object of the type is instantiated or a static method is called. And will only run once

The public constructor is accessible to all other types

The internal constructor is only accessible to types in the same assembly

On top of these three there's also protected which is only accessible to types derived from the enclosing type

and protected internal which is only accessible to types in the same assembly or those that derives from the enclosing type

and private which is only accessible from the type itself and any nested types