Learn c#--basic grammar from scratch (II.)

Source: Internet
Author: User

Basic syntax

( Beginner's series Study tutorial )

C #, aka CSharp, the celestial likes to call C well.

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

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

Let's take a look at the implementation of a rectangle class and use this set to discuss the basic syntax of C #

1 usingSystem;2 namespacerectangleapplication3 {4     classRectangle5     {6         //member Variables7         Doublelength;8         Doublewidth;9          Public voidacceptdetails ()Ten         { OneLength =4.5; Awidth =3.5; -  -         } the          Public DoubleGetarea () -         { -             returnLength *width; -  +         } -          Public voidDisplay () +         { ASystem.Console.WriteLine ("length:{0}", length); atSystem.Console.WriteLine ("width:{0}", width); -System.Console.WriteLine ("aree:{0}", Getarea ()); -  -         } -         classExecuterecatangle -         { in             Static voidMain (string[] args) -             { toRectangle r =NewRectangle (); + r.acceptdetails (); - R.display (); the System.Console.ReadKey (); *             } $         }Panax Notoginseng     } -}

Compilation Result:

Using keyword

The first statement in any C # program is:

using System:

The Using keyword is used to include namespaces in your program, and 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 a/* and terminates with the character */.

/* This is a multi-line comment for the C # programming language which is a multiline comment for the C # programming language This is a multiline comment for the C # programming language */

A single-line comment is denoted by a '//' symbol,

// This is a single-line comment for the C # language
Member variables

Variables are scholarly or data members of a class that 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 a set of tasks, and the member functions of a class are declared within a class. Our violent tears rectangle contains three member functions:

Acceptdetails, Getarea, 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 caption or underscore (_) followed by a series of captions, numbers, or underscores. The first character in an identifier cannot be a number.

Identifiers must not contain any embedded spaces or symbols, but you can use underscores.

Identifier cannot be a C # keyword

C # Keywords

Keywords are predefined reserved words for the C # compiler, which cannot have identifiers, but weaker ones you want to use as identifiers, you can prefix the keyword with the @ character.

In C #, some identifiers have special meanings in the context of the code, such as Get and set, which become the top and bottom keywords.

The following lists the reserved keywords and contextual keywords in C #;

Abstaract,as,base,bool,break,byte,case,catch,char,checked,class,const,coutinue,decimal,default,delegate,do, Double,else,enum,event,explicit,extem,false,finally,fixed,float,for,foreach,goto,if,implicit,in,int,interface, Intemal,is,lock,long,namespace,new,null,object,operator,out,override,params,private,protected,public,readonly, Ref,return,sbyte,sealed,short,sizeof,stackalloc,static,string,struct,switch,this,throw,true,try,typeof,uint, Ulong,unchedked,unsafe,ushort,using,vitual,void,volatile,while

Context keywords

Add,alias,ascending,descending,dynamic,from,get,global,group,into,join,let,orderby,partial,remove,select,set

Starting from scratch c#--basic Syntax (ii)

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.