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

0 comments:

Post a Comment

 

About Me

It's Me!Hi, I'm Moinuddin. I'm a Software Engineer at WIPRO working on Microsoft .NET Technology. I'm interested in a wide range of technology topics, mainly Microsoft including Windows Azure, WCF, WPF, Silverlight and any other cool technology that catches the eye.

Site Info

ProjectCSharp.com is mainly about demonstrating real time examples in different technologies like ASP.NET, WCF, WPF, Silverlight, Azure etc..,

Followers

Help Links

Project CSharp (C#) Copyright © 2011. All Rights Reserved.