The basic syntax of C #

The basic syntax of C #

C # is an object-oriented programming language. In object-oriented programming approach, various program objects interact with each other. The same kinds of objects generally of the same type, or that are in the same class.
For example, Rectangle (rectangular) object for example. It has a length and width attributes. Depending on the design, it may be necessary to accept these attribute values, and calculates the area to show detail.
Let's take a look at the implementation Rectangle (rectangular) class, and to discuss the basic C # syntax:

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 produces the following results:

Length: 4.5
Width: 3.5
Area: 15.75

using keywords

In any C # program in the first statement is:

using System;

using keywords for inclusion in the program namespace. A program can contain multiple using statements.

class keyword

class keyword is used to declare a class.

Comments in C #

Note for explaining code. The compiler will ignore the comment entries. Program in C #, to multi-line comment / * start, and the character to */terminate, as shown below:

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

He is represented by a single line comment symbol '@'. E.g:

}//end class Rectangle    

Member variables

It is a property or data member variable classes, for storing data. In the above procedure, Rectangle class has two member variables named length and width.

Member function

Function is a series of statements that perform specified tasks. Class member functions are declared in the class. We illustrate the Rectangle class contains three member functions: AcceptDetails, GetArea and Display.

Instantiate a class

In the above procedure, a class ExecuteRectangle Main () method of the class and instances of the class comprising Rectangle.

Identifier

Identifier is used to identify the class, variables, functions, or any other user defined items. In C #, class names must follow the following basic rules:

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

C # keywords

Keywords are the C # compiler predefined reserved words. These keywords can not be used as identifiers, but if you want to use these keywords as identifiers, you can add the @ character as a prefix in front of the keyword.
In C #, some identifiers have special meaning in the context of the code, such as get and set, these are called contextual keyword (contextual keywords).
The following table lists reserved keywords in C # (Reserved Keywords) and contextual keyword (Contextual Keywords):

Reserved Keywords
abstract as base bool break byte case
catch char checked class const continue decimal
default delegate do double else enum event
explicit external 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
Contextual keyword
add alias ascending descending dynamic from get
global group into join let orderby partial
(type)
partial
(method)
remove select set

This switched: http: //codingdict.com/article/2465

Guess you like

Origin www.cnblogs.com/bczd/p/11971047.html