C# basic syntax


Release date:2023-08-21 Update date:2023-10-13 Editor:admin View counts:328

Label:

C# basic syntax

C# is an object-oriented programming language. In the object-oriented programming method, the program consists of a variety of objects that interact with each other. Objects of the same kind usually have the same type, that is to say, in the same class medium.

For example, take a Rectangle object as an example. It has length and width Property. Depending on the design, it may need to accept these property values, calculate the area, and display details.

Let’s look at the implementation of a Rectangle class and discuss 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 results:

Length: 4.5
Width: 3.5
Area: 15.75

using Keyword

The first statement in any C# program is:

using System;

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

class keyword

class keyword is used to declare a class

C# Comments

Comments are used to interpret code. The compiler ignores annotated entries.In a C# program, multiple lines are commented to /* start with a character */ to terminate, as follows:

/* This program demonstrates
The basic syntax of C# programming
Language */

Single-line comments are made with //. For example:

} //end class Rectangle

Member variable

Variables are properties or data members of a class that are used to store data. In the above program Rectangle class has two member variables named length and width .

Member function

A function is a series of statements that perform a specified task. The member functions of a class are declared within the class. Our example classRectangle contains three member functions: AcceptDetailsGetArea and Display .

Instantiate a class

In the above program, the class ExecuteRectangle is a containing Main() methods and instancing Rectangle class.

Identifier

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

  • Identifiers must be alphabetic, underscore, or @, the beginning can be followed by a series of letters, numbers (0-9), an underscore ( _ ), @.

  • The first character in an identifier cannot be a number.

  • Identifiers must not contain any embedded spaces or symbols, such as ? - +! # % ^ & * ( ) [ ] { } . ; : " ' / \.

  • The identifier cannot be a C# keyword. Unless they have one @ prefix. For example, @if is a valid identifier, but if is not, because if is a keyword.

  • Identifiers must be case sensitive. Uppercase and lowercase letters are considered different letters.

  • Cannot be the same as the class library name of C#.

C# keyword

Keywords are reserved words predefined by the C# compiler. These keywords cannot be used as identifiers, but if you want to use these keywords as identifiers, you can add @ characters as prefixes.

In C#, some keywords have a special meaning in the context of the code, such as get and set, which are called context keywords (contextual keywords)

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

Reserved keyword

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 modifier)

Int

Interface

Internal

Is

Lock

Long

Namespace

New

Null

Object

Operator

Out

Out (generic 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

Context keyword

Add

Alias

Ascending

Descending

Dynamic

From

Get

Global

Group

Into

Join

let

Orderby

Partial (type)

Partial

Remove

Select

Set

(method)

Powered by TorCMS (https://github.com/bukun/TorCMS).