C# Classes Theory 2


This entry is part 3 of 11 in the series C# Classes

Before we can understand classes, we need to have a good knowledge of the basic building blocks.

Encapsulation means creating a boundary around an object, to separate its external (public) behavior from its internal (private) implementation details. While we are on the topic of encapsulation, what are the other pillars of object-oriented programming? There are a total of four of them: encapsulation, abstraction, inheritance, and polymorphism. A little bit later in this article, we talk about inheritance. You will need to know about inheritance before you can understand polymorphism.

Classes at Namespace Level

Your classes are defined at the namespace level. You cannot create your classes within the Main method because that is a method, or “function”.

The fundamental building block in C# is an encapsulated unit of data and functions called a type. C# has a unified type system, where all types ultimately share a common base type.

namespace Ch09Ex03  
// Classes vs Structs & Reference vs Value
    // Classes are reference types and structs are value types
{  // When you assign an object to a variable you are actually
   // assigning that variable with a pointer to the object.
    class MyClass  {
        public int val;  }
    struct myStruct  {
        public int val;  }
    class Program
    {
        static void Main(string[] args)  {
            MyClass objectA = new MyClass();
            MyClass objectB = objectA;  // objectA is a pointer
            // both variables contain pointers to the same object
            objectA.val = 10;
            objectB.val = 20;  // replaces the 10 with 20
            myStruct structA = new myStruct();
            myStruct structB = structA;
            structA.val = 30;
            structB.val = 40;
            WriteLine($"objectA.val = {objectA.val}");
            WriteLine($"objectB.val = {objectB.val}");
            WriteLine($"structA.val = {structA.val}");
            WriteLine($"structB.val = {structB.val}");
            ReadKey();
        }
    }  // output is: 20 20 30 40
}

Inheritance

Inheritance is one of the most important features of OOP. Any class may inherit from another which means it will have all the members of the class from which it inherits. The class being derived from is the parent class or the base class. Classes in C# can only derive from a single base class directly. Of course a base class can have a base class of its own and so on. From general to specific, you could have a class model as follows: food, meat, cow, prime rib. For example, Animal is a base class and Chicken is a derived class.

Private members of a base class are not accessible from a derived class, but public members are. From the viewpoint of a base class, there are three types of protection: everyone, only derived classes and nobody. Public, Protected and Private are the three keywords corresponding to everyone, only derived classes and nobody.

Series Navigation<< C# Classes Theory 1C# Class Object Initializers >>