Day 5: C# - Basic Program SyntaxDay 5: C# - Basic Program Syntax
0 0
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
  1. 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(); 
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

About Author

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *