C# Tutoriallogin
C# Tutorial
author:php.cn  update time:2022-04-11 14:06:23

C# basic syntax



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

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

Let's take a look at the implementation of a Rectangle class and discuss the basic syntax of C#:

Example

using System;
namespace RectangleApplication
{
    class Rectangle
    {
        // 成员变量
        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();
        }
    }
}

Run Instance»

Click the "Run Instance" button to view the online instance

##When the above code is compiled and executed, it produces the following results:

Length: 4.5
Width: 3.5
Area: 15.75

using Keywords

The first statement in any C# program is:

using System;

using Keywords are used in The program contains namespaces. A program can contain multiple using statements.

class Keywords

class Keywords are used to declare a class.

Comments in C

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

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

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

}//end class Rectangle

Member variables

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

Member Function

A function is a series of statements that perform specified tasks. Member functions of a class are declared within the class. Our example class Rectangle contains three member functions: AcceptDetails, GetArea and Display.

Instantiate a class

In the above program, class ExecuteRectangle is a class that contains the Main() method and instantiation of Rectangle Class of classes.

Identifier

Identifier is used to identify a class, variable, function or any other user-defined item. In C#, class naming must follow the following basic rules:

  • The identifier must start with a letter and can be followed by a series of letters, numbers (0 - 9) or underscores (_ ). The first character in the identifier cannot be a number.

  • The identifier must not contain any embedded spaces or symbols, such as ? - +! @ # % ^ & * ( ) [ ] { } . ; : " ' / \. But , an underscore ( _ ) may be used. The

  • ## identifier cannot be a C# keyword

  • ##C# keyword. Words 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 prefix the keywords with the @ character. #In C#, some identifiers have special meaning in the context of the code, such as get and set. These are called contextual keywords.

The following table lists the reservations in C#. Keywords (Reserved Keywords) and contextual keywords (Contextual Keywords):

保留关键字
abstractasbaseboolbreakbytecase
catchcharcheckedclassconstcontinuedecimal
defaultdelegatedodoubleelseenumevent
explicitexternfalsefinallyfixedfloatfor
foreachgotoifimplicitinin (generic
modifier)
int
interfaceinternalislocklongnamespacenew
nullobjectoperatoroutout
(generic
modifier)
overrideparams
privateprotectedpublicreadonlyrefreturnsbyte
sealedshortsizeofstackallocstaticstringstruct
switchthisthrowtruetrytypeofuint
ulonguncheckedunsafeushortusingvirtualvoid
volatilewhile




上下文关键字
addaliasascendingdescendingdynamicfromget
globalgroupintojoinletorderbypartial
(type)
partial
(method)
removeselectset