C # Basic syntax

Source: Internet
Author: User

C#Basic Syntax

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

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

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

Using System;Namespace Rectangleapplication{ Class Rectangle { Member variables DoubleLength; DoubleWidth; Public void Acceptdetails() {Length= 4.5;Width= 3.5; } Public Double Getarea() { ReturnLength*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. Displayconsole. Readline} }} 
/span>

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

length:4.5width:3.5area:15.75
usingKey words

The first statement in any C # program is:

UsingSystem; 

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

classKey words

The class keyword is used to declare a category.

Comments in C #

Comments are used to interpret the code. The compiler ignores the entry for the comment. In a C # program, a multiline comment starts with/* and terminates as a character, as follows:

/* This program demonstratesthe basic syntax of C # programming Language */

A single-line comment is denoted by a '//' symbol. For example:

}//end class Rectangle    
Member variables

A variable is a property or data member of a class that is used to store data. In the above program, theRectangle class has two member variables, named length and width.

member functions

A function is a series of statements that perform a specified task. The member functions of a class are declared within a 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 instantiates the Rectangle class.

Identifier

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

    • Identifiers must begin with a letter or underscore (_) followed by a series of letters, Numbers (0-9), or underscores (_). The first character in an identifier cannot be a number.
    • The identifier must not contain any embedded spaces or symbols, such as? - +! @ #% ^ & * () [] {}. ; : " ‘ / \。 However, you can use the underscore (_).
    • The identifier cannot be a C # keyword.
C # Keywords

The keyword is a predefined reserved word for the C # compiler. These keywords cannot be used as identifiers, but if you want to use these keywords as identifiers, you can prefix them with the @ character before the keyword.

In C #, some identifiers have special meanings in the context of the code, such as Get and set, which are called contextual keywords (contextual keywords).

The following table lists the reserved keywords (Reserved Keywords) and the 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 Inch 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 keywords
Add Alias Ascending Descending Dynamic From Get
Global Group Into Join Let By Partial
(type)
Partial
(method)
Remove Select Set
            

C # Basic syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.