C# Classes Theory Part 2

Data in C# is stored in a variable in one of two ways, depending on the type of variable. There are reference variables and value variables. Value types store themselves and their content in one place in memory. Reference types hold their name and a pointer to some other place in memory where their content is held. That other place is called the heap.

An int (integer) is a value type. Strings are reference types. When declaring these variables in C# you don’t have to do anything special. Its done for you. Value types always contain a value but reference types can be null. It is possible to create a value type that can contain null by using nullable types. Generic types is a topic that includes nullable types.

The only simple types that are reference types are string and object, although arrays are implicitly reference types as well. Every class you create will be a reference type.

Encapsulation means creating a boundary around an object, to separate its external (public) behavior from its internal (private) implementation details.

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}");
    }  // output is: 20 20 30 40


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.

Leave a comment

Your email address will not be published. Required fields are marked *