28

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()
 {
 }
J. Steen
  • 15,470
  • 15
  • 56
  • 63
Shankaranarayana
  • 583
  • 1
  • 6
  • 10

5 Answers5

15

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

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • protected internal which is only accessible to types in the same assembly OR derived from the enclosing type(other assembly), not "that derives...". – Reza ArabQaeni Jan 17 '18 at 18:52
8

The difference between public and internal is that the internal constructor can only be called from within the same assembly, while the public one can be called from other assemblies as well.

static is a constructor that gets called only the first time the class is referenced. Static members do not belong to an instance of the class, but "to the class itself". See http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx for more information about static.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
4
  • Static constructor runs only ones - before the first use of the class and it can access only the static members of the class
  • Public constructor runs every time when you create an object of the class using new
  • Internal is just another access modifier for the constructor above. It can also be private as well. This is exactly the same as access modifiers for other functions.

Your code doesn't actually compile, because the internal and the public one are the same constructor with different modifiers, which you can't do. You need to pick either internal or public (or private).

Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
  • public constructors don't run every time you create an object. Since the object could be created using another public constructor or a constructor with another access modifier. It's also possible to create an object without `new`. When you box a valuetype you create an object but don't use new – Rune FS Dec 07 '18 at 16:33
4

The static constructor is called the first time the type is used. Either in a static context or by creating an instance.

All other constructors are called when a new instance is created. The modifier just determines which code can create an instance.

If your constructor is private only the class itself and nested types can create an instance (maybe in a static factory method). This works like public/private/internal on methods.

Zebi
  • 8,682
  • 1
  • 36
  • 42
  • 1
    privates are also exposed to nested types – Rune FS Aug 10 '11 at 07:25
  • Is there any way to force the "use" of a generic type T without constructing an instance? – supercat Aug 10 '11 at 21:51
  • 1
    I don't understand your question. You can declare a static method with a generic Parameter: `public static T Get()`. This way you have to specify the generic parameter when calling the method. If your class has a generic Parameter you have to specify it on the class. Like `SomeClass.Do()`. – Zebi Aug 11 '11 at 11:38
  • @Zebi: If a variety of classes are designed to be returned by `Factory.Get`, and if the static constructor for each of them registers itself with `Factory`, then `Factory.Get` will be able to operate on any such type `T` whose static constructor has run. I don't know any way other than Reflection, however, via which `Factory.Get` can work if `Foo` lacks a public default constructor and hasn't run its public constructor. Note that merely passing `Foo` as a generic parameter, or even creating a `Foo[]`, won't suffice. – supercat Dec 15 '13 at 23:49
0

You do not need to create all types of constructors. The access modifiers serve the same function as any other access modifier - to determine how the constructors can be accessed.

  • a static constructor will be called the first time the class is accessed statically.
  • a constructor with an internal access modifier can only be called by items that meet the criteria for internal, which is "accessible only within files in the same assembly".
  • a constructor with an public access modifier can be accessed by anything
  • and so on. protected and private constructors operate as you expect - the constructors are accessible to items that meet the criteria for the access modifier.
foxy
  • 7,599
  • 2
  • 30
  • 34