Read Time:51 Second
C# is an object-oriented programming language.
Output Screen
Syntax:
using System;
namespace HelloWorld.RectangleApplication
{
public class Rectangle
{
// member variables
double length;
double width;
public void Acceptdetails()
{
length = 2.5;
width = 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());
}
}
}
//program.cs
using HelloWorld.RectangleApplication;
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
Console.ReadLine();
Length: 2.5
Width: 5
Area: 12.5
- using system
The using keyword is used for including the namespaces in the program. A program can include multiple using statements.
2. namespace HelloWorld.RectangleApplication;
To specify the namespace so that later others can use this class with the help of using keyword.
3. class Rectangle
class keyword is used for declaring a class.
4. Member Variables
double length;
double width;
5. Member function
Acceptdetails();
Display();
GetArea();
6. Comments in C#
/*
Multiline Comment
*/
// Single Line
7. Instantiating Class
Rectangle r = new Rectangle();
8. Call Member Function
r.Acceptdetails();
r.Display();
Console.ReadLine();