Thursday, 9 October 2014

Introduction to C# Classes

A class is a group a related methods or you can say a class is a blue print. An 
object is a instance of that class. you can make as many objects of a class
and access methods and variables of that class. 

Here in this example Bike is a class and Describe and color are the methods of 
that class. We have create a object of Bike class in Main Method and call the 
functions here.
 
 
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Bike bike;
            bike= new Bike ("Red");
            Console.WriteLine(bike.Describe());
            bike= new Bike ("Green");
            Console.WriteLine(bike.Describe());
            Console.ReadLine();

        }
    }

    class Bike
    {
        private string color;
        public Bike(string color)
        {
            this.color = color;
        }
        public string Describe()
        {
            return "This Bike is " + Color;
        }
        public string Color
        {
            get { return color; }
            set { color = value; }
        }
    }
}

No comments:

Post a Comment