C# Class - Declaration and Instantiation


Introduction to Classes

Support for classes and objects(created from those classes) is the core construct of any C# program.

And indeed is what makes C# an object-oriented programming language.

Classes are templates for Object creation. Classes allow us create larger more organized programs that are easier to maintain.

In that 1970s and 1980s, transition from sturctured, control-flow based programs to object-oriented programs revolutionized programming since it provided an extra-level of orgnanization.

Proframs were simplified and it was easier than ever to create large programs due to this better organization.

Classes are the most fundamental construct in Object-Oriented programs. It is the clases that form the abstraction or template of a real-world concept.

Declaring Classes

To declare a class is to define a class.

We use the the class keyword for this.

Here's how you define a class:

class Spaceship
{

}
  1. First you specify the class keyword.
  2. You follow it with the identifier or name of the class. e.g Spacecraft.
  3. Then curly braces to signify the class body. This is where the code that belongs to the class will be written.

Most of the time people prefer to place each class in its own file. I's not a requirement, however it's normally recommended unless the class you create is super simple.

If you place a class inside it's own file then the convention is to always name the class as you named the file.

Having defined a class, you can:

  1. declare a variable of that type.
  2. define a method that takes as a parameter or returns that type.
 class Program
    {
        static Spaceship GetLocation(Spaceship spaceship)
        {
            return spaceship;
        }
        static void Main(string[] args)
        {
            Spaceship casini, kepler;
        }
    }

Instantiating classes

So far we've created a class. However classes are just templates for creation of objects.

C# is an object-oriented programming language, and it is the objects that do stuff.

An object is basically an instance of the class. Classes mold what objects do.

The process of creating an object from a class is what is called insantiation.

To create or instantiate a class to produce an object, we use the new keyword in C#.

using System;

namespace MrClass
{
    class Spaceship
    {

    }
    class Program
    {
        static Spaceship GetSpaceship(Spaceship spaceship)
        {
            return spaceship;
        }
        static void Main()
        {
            Spaceship casini = new Spaceship();
            Spaceship kepler;
            kepler = new Spaceship();
            Console.WriteLine(GetSpaceship(casini).GetType());
            Console.ReadLine();
        }
    }
}

Result

MrClass.Spaceship
  1. The assignment can occur in the same statement or in a separate statement.
  2. The new keyword tells the runtime to allocate memory for a Spaceship object, instantiate the object and return a reference to the instance.
  3. Memory gets allocated by the runtime when we explicitly use the new keyword to create an object, however deallocation is handled automatically be the runtime without us doing anything. This, the runtime does after an object is marked inaccessible. This deallocation is handle by the garbage collector. It checks which objects are no longer referenced by other active objects and then deallocates the memory of those objects.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post