Home >Backend Development >C#.Net Tutorial >[C# Tutorial] C# Basic Syntax

[C# Tutorial] C# Basic Syntax

黄舟
黄舟Original
2016-12-26 13:48:041540browse

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. 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 look at an implementation of the Rectangle class and discuss the basic syntax of C#:

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();
        }
    }
}

When the above code is compiled and executed, it will produce the following Result:

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 statements.

class keyword

class keyword is 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 follows:

/* 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 Functions

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, 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 items. In C#, class naming must follow the following basic rules:

identifiers 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.

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

identifiers cannot be C# keywords.

C# keywords

keywords are reserved words predefined by the C# compiler. These keywords cannot be used as identifiers. , however, 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 (contextual keywords)

The following table lists the reserved keywords (Reserved Keywords) and contextual keywords (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
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    

上下文关键字    

add    alias    ascending    descending    dynamic    from    get    

global    group    into    join    let    orderby    partial
(type)    

partial
(method)    remove    select    set    

 以上就是【c#教程】C# 基本语法的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn