CategoryCreate types


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.


Interfaces are a type of contract between interface and class or struct. Interface is defined by interface keyword and the name of interface should usually start with capital letter I to make it more recognizable.


When we say Interface is a contract, what do you really mean?

Every class or struct that uses some interface must implement all of its functionalities.
Why would you use Interface at all?

There are multiple reasons actually:

Firstly, a class or classes that use Interface must use all of Interfaces functionalities. Secondly, names of all properties, methods… must be same in all classes as they are in that specific Interface. Furthermore Interfaces are obligatory, imagine situation where you must in a few classes use 3 methods Get, Insert and Delete, when you use interface with your classes there is no possibility of forgetting to implement either of these methods. Lastly, because C# does not allow us to inherit multiple classes but allows us to implement multiple Interfaces, we can create multiple interfaces where each interface inherits a class and use these interface as workaround.


What can we define inside Interface?



Methods (but without implementation)



What are Interface limitations?

Interfaces cannot define:





We cannot make interface or its members static


What about access modifiers?

By default all interface members are public and we cannot change it.


Can we instantiate Interface? Yes we can, limitation is that you must instantiate them using a class or struct that implements that interface.


Is there a limit to how many Interfaces can we implement at the same time? Yes and no. Although language specification state that you can implement as many Interfaces as you like, there are few technical limitations: heap size and metadata tables size, but worry not, you will never exceed that limit.


Please, drop by tomorrow to see a few Interface examples .

Explicit Interface member implementations

Implicit Interface member implementations







Methods are used to for plenty of reasons:

  • Cleaner code
  • Code re-usability
  • Easier maintenance
  • Code readability


How does method actually look like?

Method consists of method signature and method body.


Method signature signature always look like this:

  • Access modifier
  • Return type or void
  • Method name
  • Parameters



And After method signature we have

  • Opening curly brackets
  • Method body/our logic
  • Closing curly brackets



Let’s dissect this method:


Our method could be void, meaning that method will not return any value.


Of course, our method can also be parameter-less, contain multiple parameters or with different access modifiers. We could also have multiple methods with same name but will have to have different parameters or at-least different order of same parameters.



Classes are your blueprints. Inside classes you place all your data types and logic. Class can be recognized by class keyword. Multiple classes can exist inside one namespace. So far you have probably heard of an object, object is an instance of an class, and many objects can be created from one class. As mentioned in previous post we know that class is an reference type.

How do we create class?


We said that „Inside classes you place all your data types and logic“. Ok… so how can we use it? First, we start by creating our custom class which will have an method that will print full name in console window, then we will call an instance of that class from our second class and supply method from there with parameters.

I hope you learned something from this article and that you will visit tomorrow when we will continue with operators.

C# Reference types & Value types

In C# universe we know of two kind of types: value types and reference types. When dealing with value types, one variable points to one value, two variables can have the same value but changing value of one variable will not change value of second value. Two or more variables of reference types can contain same value, but when you change value of one variable all other variables are changed too. Values of value types are stored on stack (size of heap is 1Mb), and reference types values are stored on heap (size limitation is 2GB on 32bit system, and pretty-much unlimited on 64bit systems). Stack is designed to be fast and revolves around first in last out principle, we store value types inside stack for fast access and when we do not expect object to live long. Heap, looking at it’s size, is designed to hold bigger variables and variables inside heap can survive longer, meaning we can access it for longer periods of time.
So…how will I know which type is value or reference type? It’s really simple actually. By looking at the type’s name.

Value types Reference types
bool class
byte interface
char delegate
decimal object
double string
enum class

In next post first we will cover classes (our second reference type) and then cover life-cycle of both value types and reference types.

Value Types Part 2 – Enumeration

Through this article we will talk about what are enum (enumeration) , how and where we can use it. The enum is a keyword which we use when we want to associate some constant values with a name. Enumeration are strongly typed and appear inside IntelliSense and therefore save a lot of time 🙂  By default enumeration are zero based so first value is equal to 0 and each following is incremented by one. You can change starting and all other values to your liking, but remember that all subsequent values will also be incremented starting of last custom value. Enough of theory, lets jump to examples.

Example 1

Lets say we want to print to Console numerical day of the week.

There is nothing wrong with this example. But, what if workweek starts on some other day? You will have to change all values. How can we avoid this happening to us? We can use enums!

We declared enum with enum keyword, then we gave it a meaningful name, then inside curly brackets we wrote days of the week. To display numeric value I have explicitly changed value to int with (int). Also order of days is slightly change and Sunday is now on the first place so you should get changed output when you run the program.

Example 2

What if I want to give custom value to my enums? It’s easily accomplished. We will us our previous example.

As you can see Sunday has a value 1 and next three days have incremented by 1, but when we got to Thursday its value changed to 8, Friday got incremented to 9.  In Saturday case we set its value to Thursday + Friday and got output of 17 just to show you that option as well.


That covers enums. Stay tuned for more!




© 2019 LearnC#WithDarko

Theme by Anders NorenUp ↑