Thursday 2 November 2017

Constructors in C#

C# (C-Sharp) is an object oriented programming language or OOP for short. Many modern language such as C++, Objective-C, Java, Ruby and Python are also object oriented. You can think of an object as a sort of package that wraps up data – that’s variables, such as strings and numbers – and behaviour – that’s functions or methods that do something, often by manipulating an object’s internal data.

The definition of an object is called its class. You can think of a class as a blueprint for an object. It’s just like the blueprint for a car. The blueprint defines all the fundamental features of a car but you can’t actually drive the blueprint. To use a car you have to create one based on the blueprint’s definition. That’s what you do in programming too. You write a class and you create usable objects from it. In fact, just as you can create many cars based on a single blueprint, so you can create many objects based on a single class.

When you create a new object from a class, you can call that class’s constructor to initialize the object. A constructor is a method that has the same name as the class itself. This class is called MyClass, so the constructor method will also be called MyClass.

public MyClass( ) {
}

The constructor has to be public – that is, it has to be visible to code outside the class itself – because clearly I need to call the constructor method from other parts of my program, whenever I want to create a new MyClass object.

A constructor doesn’t have to have any parameters. However, when an object has some internal fields, it is quite common for the constructor to define a list of parameters to initialize those fields. In that case, when you invoke the constructor, to create a new object, you would need to pass the appropriate arguments to initialize those fields. When an object descends from another object and the object from which it descends has a constructor that initializes some fields, the descendent class’s constructor can call the ancestor class’s constructor, passing to it any arguments required by the ancestor constructor to initialize its fields. It does that by putting a colon after the parameter list of the constructor, then the keyword base, then a comma-separated list of arguments between parentheses.

This short video (taken from my course ‘Learn C# Programming (in ten easy steps)’ gives a few more details about C# constructors.


This course was first created in 2012 but now, in 2017, it has been completely remade. Every single video has been re-done and no less than 86 new video lessons have been added. What’s more, students can also download the entire version 1 of the course (almost 4 more hours of video) as an added bonus. This is what the course contains:

  • 117 video lessons
  • 7 hours, 7 minutes of video
  • 32 sample projects
  • 10 quizzes
  • 89-pages of pdf documentation
  • PLUS: The entire version 1 course as a free download!

The regular price for this course is $145. But use the link below to sign up for just $35 (valid only until the end of 2017, so don’t wait!)

https://www.udemy.com/learn-c-sharp-programming-in-ten-easy-steps/?couponCode=35DOLLARDEAL