CSharp Class, Object

C# is an object oriented language which means that each and every functionality of a C# program is written inside some CSharp Class. The Main method which is the starting point of any C# program is also written inside some class. This article explains what classes and objects are.

CSHarp Class, Sub Class and Objects

CSharp Class

CSharp Class, like structures, let you define your own data type. A class has no physical existence in the memory of a computer. It only provides template for the data type. Class can be seen as a map which only provides a blueprint for the design of the house, it is not the actual house, yet it is prerequisite to building an appropriate house.Unlike structures, a class data type is a reference type. A class can hold member variables and functions.

CSharp Object

Strictly speaking, an object is anything that has some characteristics and can perform some functions. In C#, an object is physical representation of a class. An object implements all the characteristics of it class. Consider following example to understand how a class can be defined and its object can be created in C#.

´╗┐using System;
namespace CSharpTutorials {

    public class Vehicle {
        public string name;
        public int price;
        public int model;
        public Vehicle(string vname, int vprice, int vmodel) {
            name = vname;
            price = vprice;
            model = vmodel;
        }
        public void DisplayInfo() {
            Console.WriteLine("\nThe name of the vehicle is:"+name);
            Console.WriteLine("The price of the vehicle is:" + price);
            Console.WriteLine("The model of the vehicle is:" + model);
        }
    }
    class Tutorial {
        static void Main(string[] args) {
            // Creating objects of Vehicle class
            Vehicle car1 = new Vehicle("Ford", 230000, 2000);
            Vehicle car2 = new Vehicle("BMW", 345000, 2005);

            car1.DisplayInfo();
            car2.DisplayInfo();
            Console.ReadKey();
        }
    }    
}

Download the code

In the above code a class named Vehicle has been declared which contains three member variables name,price and variable. It also contains a public method Vehicle which has no return type (not even void). This is the constructor. Whenever the object of a class is created this constructor method is called. You can also pass parameters to the constructor as has been done in the above code. This constructor initializes member variables of the Vehicle class. The class, also contains a member function called DisplayInfo which displays the values of the three member variables of the vehicle class.

Inside the Main function two objects of the Vehicle class have been created namely car1 and car2. Notice, these objects have been created via new keyword followed by the parameterized constructor of the vehicle class. Then using these objects, the DislayInfo method is called via dot operators.

<<< Functions in C#Inheritance in C# >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .