TagConstructor

Assembly class

Assembly class allows us to:

  • Load assemblies
  • Explore metadata and types
  • Create instance of types

Easiest way to load assembly is by load them using following methods :

GetAssembly (requires Type variable)

GetCallingAssembly (returns Assembly from which this method has been called)

These two methods have Assembly return type. After acquiring Assembly variable we can use it to get types that we want using GetType method.

Let’s explore Assembly class:

 

Constructor

Constructors are a public methods with the same name as a class were they resides. Constructors are always created with a class, weather we write it ourselves or if class creates it for us(although it will not be visible ). Role of constructors is to initialize fields when we instantiate class. It is possible to create multiple constructors. Adding „: this()“ after parameters of constructor with parameters will execute default constructor before constructors with parameters. To easily create constructors type ctor keyword an new one will be generated for you.

 

If you have ever initialized class you have consumed constructors in one way or another. Example of consuming constructors would be in previous example where we have instantiated ClassWithConstructor class with string “Mike” and integer 1.

 

Is it possible to forbid class to initialize constructor?

Yes it is, just create empty one and set private access modifier.

 

What about other access modifiers? Can I use them all?

Public, private, protected, internal, or protected internal access modifiers are allowed to be used with constructors.

 

How can i initialize base class constructors ?

Simply type

after constructors parameters . Remember, you don’t have to supply parameters for base class constructor  because base constructor will always be called before derived class constructor, weather we call it or not.

© 2021 LearnC#WithDarko

Theme by Anders NorenUp ↑