Object Orientation with C# using Visual Studio 2010

About this Tutorial –

Objectives –

Delegates will learn to develop web applications using C# 4.0. After completing this course, delegates will be able to:

  • Use Visual Studio 2010 effectively
  • Object Orientation with C# using Visual Studio 2010


This course has been designed primarily for programmers new to the .Net development platform. Delegates experience solely in Windows application development or earlier versions of ASP.Net will also find the content beneficial.


Before attending this workshop, students must:

  • Be able to manage a solution environment using the Visual Studio 2010 IDE and tools
  • Be able to program an application using a .NET Framework 4.0 compliant language


Copyright 20/12/12 – David Ringsell

Download Solutions

Java tutorial

Lab 1 – Classes and Objects

  1. Create a class library project called Date:
    • Open Visual Studio 2010 and select the File menu, then New, then Project.
    • Make sure (if you are using the Professional Version) that Visual C# and then Windows is selected at the left of the screen under Installed Templates.
    • Name the project Date and the solution: DateTest.
    • Browse to the place on your computer where you wish Visual Studio to create the directory for your solution (keeping the tick box selected).
    • Select Class Library and then click OK.
    • Rename the Class1.cs file Date.cs.
  2. Define a class called Date in the project. Add a constructor and also define a method called DisplayDate() in the class.
  3. Add a second overloaded constructor to the Date class. This will initiate the month and days properties only in the class, the year will default to the current year.
  4. To add three properties for the year, month, day, to the Date class.
    public class Date
       // Properties
       public int Year { get; set; }
       public string Month { get; set; }
       public int Day { get; set; }
       // Constructer
       public Date(int intYear, string strMonth, int intDay)
          Year = intYear;
          Month = strMonth;
          Day = intDay;
       // Overloaded Constructer
       public Date(string strMonth, int intDay)
          Year = 2011;
          Month = strMonth;
          Day = intDay;
       // A method to display the date
       public void DisplayDate()
          Console.WriteLine(String.Format("The Date is: {0} {1} {2}",
          Day.ToString(), Month, Year.ToString()));
  5. View code file.
  6. Add a new console application project to the solution:
    • Right-click on the project name: Date.
    • Select Add.
    • Select New Project.
    • Make sure the location of the project is within the solution folder: DateTest.
  7. Add a reference in the console application to the Date class library.
    • Go to the Project Menu or right-click on the project name.
    • Select Add Reference.
    • Select the Browse tab and navigate to and select the Date.dll file within the bin/Debug folder within the Date folder (which contains your solution).
    • Type using DateTest; into the beginning of the Tester.cs code file. DateTest is the namespace and solution name.
  8. Create an oDate object from the Date class, in the console application.
    // Create an instance of the date object
    Date oDate = new Date(1968, "July", 18);
  9. Test the methods and properties of the oDate object.
    // Change the property values in oDate
    oDate.Year = 1977;
    oDate.Month = "October";
    oDate.Day = 25;
    // And then display them
  10. View code file.

Lab 2 – Inside Methods

  1. Add a GetDate method with ref parameters for year, month, day, to the Date class created in Lab 1.
  2. Add code to GetDate() to return valid parameters for year, month, day.
    // A method to return the date
    public string GetDate(ref string day, ref string month, ref string year)
       day = intDay;
       month = strMonth;
       year = intYear;
       return (String.Format("The Date is: {0} {1} {2}",
       intDay.ToString(), strMonth, intYear.ToString()));
  3. Test the method by calling it, then displaying the returned year, month, day values.
    int intDay = 0;
    string strMonth = "";
    int intYear = 0;
    // Display the values in the first date object using the GetDate method
    Console.WriteLine(oDate.GetDate(ref intDay, ref strMonth, ref intYear));
    // Show how the GetDate method has changed the local variables by using ref
    Console.WriteLine("The Date is: " + intDay.ToString() + " " + strMonth + " " + intYear.ToString());
  4. View code file.

Lab 3 – Debugging

For all remaining lab exercises throughout this course, examine and debug your code by:

  1. Setting breakpoints in lines of code by clicking in the margin. When run the code will pause at the first beakpoint.
  2. Stepping through lines of code when a breakpoint is hit by pressing F11. The code will execute line by line. Hover the mouse pointer over a variable to show its current value.
  3. Using these debug windows to understand and fix code:
    • Watch
    • Locals
    • Immediate
    • Call Stack
  4. Debug windows are available only when the code is paused in break mode. Use the Debug>Windows command to show them.

Lab 4 – Inheritance

  1. Open Visual Studio and create a new C# Console Application project.
  2. Define a class called Control, with a constructor and a DrawControl() method.
  3. Add only a Console.Write() statement to the DrawControl() method.
    public class Control
       // These members are private and thus invisible
       // to derived class methods; we'll examine this
       // later in the chapter
       private int top;
       private int left;
       // Constructor takes two integers to
       // fix location on the console
       public Control(int top, int left)
          this.top = top;
          this.left = left;
       // Simulates drawing the window
       public void DrawControl()
          Console.WriteLine("Drawing Control at {0}, {1}",
          top, left);
  4. Create a class called TextBox that inherits from DrawControl, but replaces the DrawWindow method. Change the Console.Write() statement to show a different message.
    // TextBox derives from Control
    public class TextBox : Control
       // A new version (note keyword) because in the
       // derived method we change the behavior
       public new void DrawControl()
          base.DrawControl(); // Invoke the base method
          Console.WriteLine("Writing string to the textbox: {0}",
  5. Test the TextBox class by calling its DrawControl method.
    // Create a base instance
    Control contr = new Control(5, 10);
    // Create a derived instance
    TextBox txt = new TextBox(20, 30, "Hello world");
  6. View code file.

Lab 5 – Operator Overloading

  1. Open Visual Studio and create a new C# Console Application project.
  2. Define a class called Fraction, with a constructor and a ToString() method.
  3. Add two integer field to the class to hold the numerator and the denominator.
  4. Add a constructor to initialize the numerator and the denominator.
  5. Create a subtraction operator (-) using operator overloading.
  6. Test the operator.
  7.   Your code may look something like this:

    public class Fraction
       private int numerator;
       private int denominator;
       // Create a fraction by passing in the numerator
       // and denominator
       public Fraction(int numerator, int denominator)
          this.numerator = numerator;
          this.denominator = denominator;
       // Overloaded operator- takes two fractions
       // and returns their sum
       public static Fraction operator -(Fraction lhs, Fraction rhs)
          // Like fractions (shared denominator) can be added
          // by adding thier numerators
          if (lhs.denominator == rhs.denominator)
             return new Fraction(lhs.numerator - rhs.numerator,
          // Simplistic solution for unlike fractions
          int firstProduct = lhs.numerator * rhs.denominator -
          rhs.numerator * lhs.denominator;
          int secondProduct = lhs.denominator * rhs.denominator;
          return new Fraction(firstProduct, secondProduct);
       // Return a string representation of the fraction
       public override string ToString()
          String s = numerator.ToString() + "/" +
          return s;
    Fraction f1 = new Fraction(1, 2);
    Fraction f2 = new Fraction(1, 6);
    // Test the overloaded minus operator
    Fraction f3 = f1 - f2;
    Console.WriteLine("f1 - f2 = f3: {0}", f3.ToString());
  8. View code file.

Lab 6 – Structs

  1. Open Visual Studio and create a new C# Console Application project.
  2. Define a struct called Colour, with a constructor and a ToString() method.
  3. Add three integer properties to represent the red, green and blue component of the colour.
    public struct Colour
       public int red { get; set; }
       public int green { get; set; }
       public int blue { get; set; }
       // Constructor
       public Colour(int rVal, int gVal, int bVal) : this()
          red = rVal;
          green = gVal;
          blue = bVal;
       // Display the Struct as a String
       public override string ToString()
          return (String.Format("{0}, {1}, {2}", red, green, blue));
  4. Test the struct.
    // Create an instance of the struct
    Colour colour1 = new Colour(100, 50, 250);
    // Pass the struct to a method
    // Display the values in the struct
    Console.WriteLine("colour1 colour: {0}", colour1);
    // Method takes a struct as a parameter
    public void myFunc(Colour col)
       // Modify the values through the properties
       col.red = 200;
       col.green = 100;
       col.blue = 50;
       Console.WriteLine("colour1 colour: {0}", col);
  5. View code file.

Lab 7 – Interfaces

  1. Open Visual Studio and create a new C# Console Application project.
  2. Define an interface called IClient.
    // Define the interface
    interface IClient
  3. Add properties for name and address details and a method called Order(), to the interface.
    void Order(int orderNum);
    string name { get; set; }
    string address { get; set; }
  4. Create a second class called Customer that implements all the interface’s members.
    // Create a Customer class that implements the IClient interface
    public class Customer : IClient
       public Customer(string s)
          Console.WriteLine("Creating a New Customer ID: {0}", s);
       // Implement the Order method
       public void Order(int newOrder)
          Console.WriteLine("Implementing the Order Method for IClient.
          The Order Number is: {0}", newOrder);
       // Implement the properties
       public string name { get; set; }
       public string address { get; set; }
  5. Test the Customer class.
    Customer cust = new Customer("H56388");
    cust.name = "Brian Ferry";
    cust.address = "23 Orange Lane, Clifton, Bristol, BS6 5FH";
    Console.WriteLine("The Name of the Customer is: {0}", cust.name);
    Console.WriteLine("The Address of the Customer is: {0}", cust.address);
  6. View code file.

Back to the beginning

If you liked this post, please comment with your suggestions to help others.
If you would like to see more content like this in the future, please fill-in our quick survey.
Scroll to Top