T B H P N

C# Class Structure

Synopsis:

The C# language was built, from the ground up, to support user defined reference types.
It does not provide copy constructors, nor does it support overloading the assignment operator. You cannot expect to make copies of instances of a C# class. Instead, you make copies of references to the same instance on the managed heap.
You can only expect to make copies of value types, e.g., primitive types, enums, and structs. For these types, copies are made by copying their byte representation to a new named location.
If a C# class has only value type members, e.g., primitive types, enums, and structs, you can make a clone by creating a new instance of the class and copying the value type members of the source. Finally, clones may be constructed using reflection, but that is an arduous process1.
Here is a demonstration class that has a very typical structure, including a public property, private integer member, and methods for construction and display.

Code in File ClassDemo.cs

  /////////////////////////////////////////////////////////////////////
  // ClassDemo.cs - basic class anatomy                              //
  //                                                                 //
  // Jim Fawcett, CSE681 - Software Modeling and Analysis, Fall 2017 //
  /////////////////////////////////////////////////////////////////////

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;

  namespace CSharpClass
  {
    public class X
    {
      public X(int count = 0)                                // default argument supports default construction
      {
        count_ = count;
      }
      ~X()                                                   // called by the CLR finalizer
      {
        Console.Write("\n  {0} is about to die", name);
      }
      public void say()                                      // method uses name property and private int
      {
        Console.Write("\n  Hello, my name is {0} and my count is {1}", name, count_);
      }
      public string name { get; set; } = "instance of X";    // public, but encapsulated, property
      private int count_ = 0;                                // private encapsulated member
    }
    class TestX
    {
      static void Main(string[] args)
      {
        Console.Write("\n  Simple Class Demonstration");
        Console.Write("\n ============================");

        X x1 = new X();
        x1.name = "x1";
        x1.say();
        X x2 = new X(3);
        x2.name = "x2";
        x2.say();
      }
    }
  }

Output

  Simple Class Demonstration
 ============================
  Hello, my name is x1 and my count is 0
  Hello, my name is x2 and my count is 3
  x2 is about to die
  x1 is about to die

Press any key to continue . . .

C# provides destructors that are called when the CLR garbage collector runs. This means release of resources allocated by the class is non-deterministic. The C# garbage collector is effective, frequently quickly disposing an object no longer referenced by its program. But there are no guarantees.

Example Code:


  1. Note, that the method MemberwiseClone(), every class inherits from the base Object class, defaults to cloning references, not instances.

CST strip