Inheritance in C#

Classes can inherit from another class. This is accomplished by putting a colon after the class name when declaring the class, and naming the class to inherit from—the base class—after the colon


using System;


namespace Tutorial
{
public class Parent
{
string parentString;
public Parent() // Constructor
{
Console.WriteLine("Parent Constructor.");
}
public Parent(string myString) // Constructor takes "myString" as parameter.
{
parentString = myString; // Assigns parameter to parentString
Console.WriteLine(parentString);
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class Child : Parent // Inheritance
{
public Child()
: base("From Derived") //Inherits and calls the base class constructor.
{
Console.WriteLine("Child Constructor.");
}
public void print()
{
base.print();
Console.WriteLine("I'm a Child Class.");
}
public static void Main() //Static main method, init of application
{
Child child = new Child();
child.print();
((Parent)child).print();
}
}
}

The new class—the derived class—then gains all the non-private data and behavior of the base class in addition to any other data or behaviors it defines for itself. The new class then has two effective types: the type of the new class and the type of the class it inherits.

Measure execution time

This C# code snippet measures a time interval as you would measure the execution time of a task.
using System;

DateTime startTime = DateTime.Now;
Console.WriteLine ("Started: {0}", startTime);
 
// Execute the task to be timed
for (int i=1; i < 100000; i++){}   
  
DateTime stopTime = DateTime.Now;
Console.WriteLine ("Stopped: {0}", stopTime);
 
TimeSpan elapsedTime = stopTime - startTime;
Console.WriteLine ("Elapsed: {0}", elapsedTime);
Console.WriteLine ("in hours       :" + elapsedTime.TotalHours);
Console.WriteLine ("in minutes     :" + elapsedTime.TotalMinutes);
Console.WriteLine ("in seconds     :" + elapsedTime.TotalSeconds);
Console.WriteLine ("in milliseconds:" + elapsedTime.TotalMilliseconds);

Declare simple event

This C# code snippet declares an event with EventHandler using the default implementation for removing events and subscribing to events. We implement the IDisposable interface simply to have a reasonable excuse to throw an event.
using System;
 
public class MyClass : IDisposable
{
   public event EventHandler Disposing;  
 
   public void Dispose()
   {
      // release any resources here
      if (Disposing != null)
      { 
         // someone is subscribed, throw event
         Disposing (this, new EventArgs());
      }
   }
 
   public static void Main( )
   {
      using (MyClass myClass = new MyClass ())
      {
         // subscribe to event with anonymous delegate
         myClass.Disposing += delegate 
            { Console.WriteLine ("Disposing!"); };
      }
   }
}

Versioning in C#

This sample demonstrates versioning in C# through the use of the override and new keywords. Versioning maintains compatibility between base and derived classes as they evolve.


// versioning.cs
public class MyBase
{
public virtual string Meth1()
{
return "MyBase-Meth1";
}
public virtual string Meth2()
{
return "MyBase-Meth2";
}
public virtual string Meth3()
{
return "MyBase-Meth3";
}
}

class MyDerived : MyBase
{
// Overrides the virtual method Meth1 using the override keyword:
public override string Meth1()
{
return "MyDerived-Meth1";
}
// Explicitly hide the virtual method Meth2 using the new
// keyword:
public new string Meth2()
{
return "MyDerived-Meth2";
}
// Because no keyword is specified in the following declaration
// a warning will be issued to alert the programmer that
// the method hides the inherited member MyBase.Meth3():
public string Meth3()
{
return "MyDerived-Meth3";
}

public static void Main()
{
MyDerived mD = new MyDerived();
MyBase mB = (MyBase) mD;

System.Console.WriteLine(mB.Meth1());
System.Console.WriteLine(mB.Meth2());
System.Console.WriteLine(mB.Meth3());
}
}