Click here to Skip to main content
13,901,285 members
Click here to Skip to main content
Add your own
alternative version


49 bookmarked
Posted 22 Oct 2008
Licenced CPOL

Key Object-Oriented Concepts and how to use them in Visual C#

, 15 Sep 2013
Rate this:
Please Sign up or sign in to vote.
Overview of Key Object-Oriented Concepts in Visual C# (Quick Guide)

Overview of Object-Oriented Concepts in Visual C# 

January 01, 2008



This paper is an overview of Object-Oriented concepts and how to implement them in Visual C#. We begin with an overview of Object-Oriented concepts in C# and then represent an detailed example in Visual C# that shows how to implement them.



In Visual C#, all code is contained in a class. Some members of classes are described in Table 1:






Fields store the data a class needs to fulfill its design.

Fields are declared within the class block by specifying the access level of the field, followed by the type of the field, followed by the name of the field.


private double seconds;



Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields.

Properties can be used as though they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily while still providing the safety and flexibility of methods.

public double Hours


get { return seconds /

3600; }

set { seconds = value

* 3600; }




A method is a code block containing a series of statements.

public void AddGas(int gallons) { }




An event is a way for a class to provide notifications when something of interest happens.

For example, a class that encapsulates a user interface control might define an event to occur when the user clicks on the control.

The control class does not care what happens when the button is clicked, but it does need to tell derived classes that the click event has occurred. The derived classes can then choose how to respond.



Table 1 – Some members of classes

Example: This example shows how to implement members of a class.

public class List

const int defaultCapacity = 4;


object[] items;
int count;


public List(): List(defaultCapacity) {}

public List(int capacity) {
items = new object[capacity];


public int Count {
get { return count; }

public string Capacity {
get {
return items.Length;
set {
if (value < count) value = count;
if (value != items.Length) {
object[] newItems = new object[value];
Array.Copy(items, 0, newItems, 0, count);
items = newItems;


public object this[int index] {
get {
return items[index];
set {
items[index] = value;


public void Add(object item) {
if (count == Capacity) Capacity = count * 2;
items[count] = item;

protected virtual void OnChanged() {
if (Changed != null) Changed(this, EventArgs.Empty);

public override bool Equals(object other) {
return Equals(this, other as List);

static bool Equals(List a, List b) {
if (a == null) return b == null;
if (b == null || a.count != b.count) return false;
for (int i = 0; i < a.count; i++) {
if (!object.Equals(a.items[i], b.items[i])) {
return false;
return true;


public event EventHandler Changed;


public static bool operator ==(List a, List b) {
return Equals(a, b);

public static bool operator !=(List a, List b) {
return !Equals(a, b);




If you want to create methods or properties that can be called without first creating an object, declare those items as static. (Equivalent of a Visual Basic .NET module)

Visual C# uses six key concepts for working with classes. Table 2 describes these key concepts.



In Visual C#


» You define classes using the class keyword.

» All executable code is part of a class.



» There are five levels of access to classes and their members:

  • public
  • internal
  • protected
  • protected internal
  • private

» For more information, refer to table 3



Classes can inherit members from base classes and override or overload members of the inherited class.


Constructors and destructors

» Classes have constructors and destructors that are called when an object based on the class is created or destructors.

» Constructor methods have the same name as their class.

» Destructor methods use the class name preceded by a tilde (~).



» The delegates keyword provides a safe way to call methods by their address rather than by their name.

» This is the .NET equivalent of a callback.

» Delegates are commonly used with events and asynchronous procedures.


Abstract classes and interfaces

» You can create interfaces and abstract classes.

» Interfaces define the member names and member parameter lists for classes that use the interface.

» Abstract classes provide the members to be inherited by classes derived from them.

Table 2 – Key Object-Oriented Concepts




Creating Classes and Providing Access

In Visual C#, use the class keyword to define classes. Use on of the access keywords described in table 3 to define which other classes can use the members of the current class.


Visual C#

Available to


ALL members in all classes and PROJECTS.


» All members in the current PROJECT.

» in Visual Basic: Friend


» All members in the current CLASS and in classes DERIVED from this member's class.

» Can be used only in member definitions, not for classes or modules definitions.


Protected internal

» All members in the current PROJECT and all members in classes derived from this member's class.

» Can be used only in member definitions, not for classes or modules definitions.


Members of the current class only.


Table 3 – Levels of Access for Classes




Visual C# uses the keywords described in Table 4 for creating base classes and deriving new classes from them.


Visual Basic

Visual C#

Use to


derivedclass : baseclass

Base on class on another, inheriting members from the base class.




Declares that a member of the base class can be overriden in a derived class.




Declares that a member of a derived class overrides the member of the same name in the base class.




Declares that a member of a derived class hides the member of the same name in the base class.



Declares that a class provides a template for derived classes. This type of class is called an abstract class, and it can’t be instantiated.




Declares that a member of a class provides a template for derived members. This type of member is called an abstract member, and it can’t be invoked.




Call a base class member from within the derived class.




Call a member of the current instance of a class.




Create an interface that defines the members a class must provide.



classname : interface

Use an interface definition in a class.


Table 4 – Overview of the Inheritance Keywords



Overriding, Overloading and Shadowing Members

A derived class inherits the members of its base class. A member's signature includes its name, parameter list, parameter types, and return type. If the derived class defines a member with the same signature, the derived member overrides the base member. If a derived class defines a member with the same name but a different parameter list, parameter type, or return type than the base member, the derived member either overloads or shadows the base member. A member overloads another member if the base member is still available. A member shadows another member if the derived member replaces the base member.



In the Abstract

» An abstract class is a class that provides a template for derived classes, and it can’t be instantiated.

» An abstract method is a virtual method with no implementation.

» An abstract method is declared with the abstract modifier and is permitted only in a class that is also declared abstract.

» An abstract method must be overridden in every non-abstract derived class.




Interfaces are similar to abstract classes in that they both provide a template that you can use to create new classes. The difference is that interfaces don’t provide any implementation of class members, whereas abstract classes can implement members that then become common to all the classes derived from them.


When you implement a particular interface in a class, instances of that class can be used for any argument or variable declared as that interface.



Detailed Example

Now, we represent an example that shows how to implement following class diagram in Visual C#.



Figure 1 – Class Diagram of Detailed Example


// (1): (virtual)
//      In Visual C#, members that can be overriden must be declared
//          as virtual.
// (2): (Sphere: Circle)
//      Sphere class inherits all of methods and properties defined //          in Circle.
// (3): (override)
//      Sphere overrides the methods for Area, because
//          sphere use a different formula for this calculation.
//      A virtual method can be overridden in a derived class.
// (4): (new)
//      Sphere shadows the Center method because
//          sphere have an additional coordinate (z)
//          and you wouldn't want users to accidentally set
//          xy-coordinates without setting a z-coordinate.
// (5): (base)
//      Notice that Sphere uses the base keyword to call
//          the base class's Center method within the shadowed 
//          method.

// Interface for all shapes.
public interface IFigure
    float Top
    float Left

    float Area();
    float Perimeter();
// To use the inteface, implement it in a class
public abstract class Shape : IFigure
    // Constructor
	public Shape()

    public abstract float Top
    public abstract float Left

    public abstract float Area();
    public abstract float Perimeter();
public class Circle : Shape
    float fxCenter, fyCenter, fRadius;
    // Constructor
    public Circle()
        // Intialize internal variables.
        fxCenter = 0;
        fyCenter = 0;
        fRadius = 0;

    public override float Top
            return fxCenter - fRadius;
            fxCenter = value + fRadius;
    public override float Left
            return fyCenter - fRadius;
            fyCenter = value + fRadius;
    public float Radius
            return fRadius;
            fRadius = value;

    public override float Area()
        return (float)(System.Math.PI * Math.Pow((double)fRadius, 

    public override float Perimeter()
        return 2 * fRadius * (float)System.Math.PI;

    public virtual void Center(float X, float Y)    // (1)
        fxCenter = X;
        fyCenter = Y;
public class Sphere : Circle                        // (2)
    float fCenter;

    // Constructor
    public Sphere()
        // Initialize internal variable.
        fCenter = 0;
    public float Front
            return fCenter - base.Radius;
            fCenter = value + base.Radius;

    public override float Area()                    // (3)
        return (float)(4 * Math.PI * Math.Pow((double)base.Radius, 

    public void Center(float X, float Y, float Z)
        base.Center(X, Y);                          // (5)
        fCenter = Z;

    public new void Center(float X, float Y)        // (4)
        this.Center(X, Y, 0);

    public float Volume()
        return (float)((4 / 3) * System.Math.PI * 
                       Math.Pow((double)base.Radius, 3));
public partial class _Default : System.Web.UI.Page 
    // Displays the shape info on the Web form.
    // Because the Shape abstract class in this example implements 
    //     the IFigure interface,
    //     all derived classes from Shape can be used as the 
    //     arguments of the type IFigure.
    private void ShowShapeInfo(IFigure Shape)
        // Since Shape argument is IFigure, we know it has these 
        Response.Write(String.Format("Shape Top : {0} <br>", 
        Response.Write(String.Format("Shape Left: {0} <br>", 
        Response.Write(String.Format("Shape Area: {0} <br>", 
        Response.Write(String.Format("Shape Primeter: {0} <br>", 

    protected void Page_Load(object sender, EventArgs e)
        // Create a circle
        Circle MyCircle = new Circle();
        MyCircle.Radius = 2;
        MyCircle.Center(10, 2);
        // Create a sphere
        Sphere MySphere = new Sphere();
        MySphere.Radius = 10;
        MySphere.Center(10, 20, 25);
        // Show info about each shape.



Some of the Resources

Ø      Published by Microsoft Press “MCAD/MCSD Self-Placed Training Kit: Developing Web Applications with Microsoft Visual Basic.NET and Microsoft Visual C#.NET”

Ø      Published by Microsoft Press “C# Language Specification 1.2”

Ø      Microsoft Developer Network



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Enayat Ashkian
Engineer ParsianSolutions
Iran (Islamic Republic of) Iran (Islamic Republic of)
Importance of using IT is clear as a key tool to obtain organization goals, increasing efficiency, competitiveness, management and etc. I am working on getting ready and support the field of IT usage and educating it for more than 10 years as a member (consultant) of Iranian ICT Guild Organization (IIG NGO).
Importance de l'aide, il est clair comme un outil clé pour obtenir des objectifs de l'organisation, accroître l'efficacité, la compétitivité, la gestion, etc, je travaille sur l'obtention de prêts et soutiens le domaine de l'utilisation et de l'éducation depuis plus de 10 ans en tant que membre (consultant ) de l'Organisation iranienne Guild TIC (IIG ONG).
امروزه اهمیت استفاده از فناوری اطلاعات (IT) به عنوان ابزار کلیدی در نیل به اهداف، انجام امور، افزایش بهره وری، رقابت پذیری، مدیریت و ... از بدیهیات است. اینجانب عنایت اشکیان کارشناس کامپیوتر و مشاور IT سازمان نظام مهندسی رایانه در زمینه فراهم سازی و پشتیبانی بستر و ابزار استفاده از فناوری اطلاعات و آموزش نحوه استفاده از آن فعالیت دارم

Official Web Site including detailed information about Experiences, Honors and Awards, Courses, Certifications and contact ways :

Alternative option:

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
fredatcodeproject17-Sep-13 0:52
professionalfredatcodeproject17-Sep-13 0:52 
GeneralMy vote of 5 Pin
S.P.Tiwari16-Sep-13 3:35
professionalS.P.Tiwari16-Sep-13 3:35 
QuestionMy Vote of 5 Pin
Aydin Homay15-Sep-13 23:39
memberAydin Homay15-Sep-13 23:39 
GeneralMy vote of 5 Pin
Nikhil_S15-Sep-13 22:23
professionalNikhil_S15-Sep-13 22:23 
GeneralBest Practices PinPopular
thund3rstruck24-Oct-08 15:01
memberthund3rstruck24-Oct-08 15:01 
GeneralRe: Best Practices Pin
Enayat Ashkian25-Oct-08 7:49
memberEnayat Ashkian25-Oct-08 7:49 
GeneralRe: Best Practices Pin
thund3rstruck25-Oct-08 8:16
memberthund3rstruck25-Oct-08 8:16 
GeneralRe: Best Practices Pin
Enayat Ashkian26-Oct-08 19:18
memberEnayat Ashkian26-Oct-08 19:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190306.1 | Last Updated 16 Sep 2013
Article Copyright 2008 by Enayat Ashkian
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid