C # Tutorial 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 variable        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.5width:3.5area:15.75

Using keyword

The first statement in any C # program is:

Using System;

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

class keyword

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, the Rectangle 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, 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 in in (generic
modifier) int

Interface internal is lock long namespace new

Null object operator 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's by partial
(type)

Partial
(method) Remove Select Set

The above is the "C # Tutorial" C # Basic syntax of the content, more relevant content please focus on topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.