Creating a Sample C# program using Classes and objects.


A C# program can be written by using Visual Studio or any editor. Consider the following code, which declares the Car class and creates the object MyCar of the same class:

using System;
class Car
{
    //Following are the Member variables of a class.
    string Engine;
    int NoOfWheels;
    //Following are the Member function of a class.
    public void AcceptDetails()
    {
         Console.WriteLine("Enter the Engin Model");
         Engine = Console.ReadLine();
         Console.WriteLine("Enter the number of Wheels");
         NoOfWheels = Convert.ToInt32(Console.ReadLine());
    }

    public void DisplayDetails()
    {
         Console.WriteLine("The Engine Model is:{0}", Engine);
         Console.WriteLine("The number of Wheels are:{0}", NoOfWheels);
    }

    //Class used to instantiate the Car Class.
    class ExecuteClass
    {
        public static void Main(string[] args)
        {
             Car MyCar = new Car();
             MyCar.AcceptDetails();
             MyCar.DisplayDetails);
    
             Console.ReadLine();
        }
    }

Output:
Enter the Engine Model
800CC
Enter the number of Wheels
4
The Engine Model is: 800CC
The number of Wheels are: 4

No comments:

Post a Comment

Recent Posts