helloworld.cs
Open the command prompt tool and go to the directory where you saved the
file.
Type
csc
helloworld.cs
and press enter to compile your code.
If there are no errors in your code, the command prompt takes you to the next
line and generates
helloworld.exe
executable file.
Type
helloworld
to execute your program.
You can see the output Hello World printed on the screen.
C# is an object-oriented programming language. In Object-Oriented Programming
methodology, a program consists of various objects that interact with each other by
means of actions. The actions that an object may take are called methods. Objects
of the same kind are said to have the same type or are said to be in the same class.
For example, let us consider a Rectangle object. It has attributes such as length and
width. Depending upon the design, it may need ways for accepting the values of
these attributes, calculating the area, and displaying details.
Let us look at implementation of a Rectangle class and discuss C# basic syntax:
using System;
namespace RectangleApplication
{
class Rectangle
{
// member variables
double length;
double width;
public void Acceptdetails()
{
length = 4.5;
7
C#
width = 3.5;
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result:
Length: 4.5
8
C#
Width: 3.5
Area: 15.75
Dostları ilə paylaş: |