Easy Tutorial
❮ Csharp Bitarray Csharp Nullable ❯

C# Basic Syntax

C# is an object-oriented programming language. In the object-oriented programming methodology, a program consists of various interacting objects. Objects of the same kind typically have the same type, or, in other words, are defined in the same class.

For example, let's take a Rectangle object. It has length and width properties. According to the design, it may need to accept these property values, calculate the area, and display the details.

Let's look at an implementation of a Rectangle class and use it to discuss the basic syntax of C#:

Example

using System;
namespace RectangleApplication
{
    class Rectangle
    {
        // Member variables
        double length;
        double width;
        public void Acceptdetails()
        {
            length = 4.5;    
            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
Width: 3.5
Area: 15.75

using Keyword

The first statement in any C# program is:

using System;

The using keyword is used to include namespaces in the program. A program can contain multiple using statements.

class Keyword

The class keyword is used to declare a class.

Comments in C

Comments are used to explain code. Compilers ignore the comment entries. In C# programs, multi-line comments start with /* and end with the character */, as shown below:

/* This program demonstrates
C# comments
usage */

Single-line comments are denoted by the // symbol. For example:

// This line is a comment

Member Variables

Variables are class attributes or data members used to store data. In the above program, the Rectangle class has two member variables named length and width.

Member Functions

Functions are statements that perform a specific task. Class member functions are declared within the class. The class Rectangle in our example contains three member functions: AcceptDetails, GetArea, and Display.

Instantiating a Class

In the above program, the class ExecuteRectangle is a class that contains the Main() method and instantiates the Rectangle class.

Identifiers

Identifiers are used to identify classes, variables, functions, or any other user-defined item. In C#, the naming of a class must follow these basic rules:

C# Keywords

Keywords are predefined reserved words in C# by the compiler. These keywords cannot be used as identifiers. However, if you want to use these keywords as identifiers, you can prefix them with the @ character. In C#, some keywords have special meanings in the context of the code, such as get and set, which are known as contextual keywords.

The following table lists the reserved keywords and contextual keywords in C#:

| Reserved Keywords | | abstract | as | base | bool | break | byte | case | | catch | char | checked | class | const | continue | decimal | | default | delegate | do | double | else | enum | event | | explicit | extern | false | finally | fixed | float | for | | foreach | goto | if | implicit | in | in (generic <br>modifier) | int | | interface | internal | is | lock | long | namespace | new | | null | object | operator | out | out <br>(generic <br>modifier) | override | params | | private | protected | public | readonly | ref | return | sbyte | | sealed | short | sizeof | stackalloc | static | string | struct | | switch | this | throw | true | try | typeof | uint | | ulong | unchecked | unsafe | ushort | using | virtual | void | | volatile | while | | | | | | | Contextual Keywords | | add | alias | ascending | descending | dynamic | from | get | | global | group | into | join | let | orderby | partial <br>(type) | | partial <br>(method) | remove | select | set | | | |

❮ Csharp Bitarray Csharp Nullable ❯