(菜鸟系列学习教程)
C#,又名Csharp,天朝喜欢叫C井。
C#是一种面向对象的编程语言。在面向对象的程序设计方法中,程序有各种相互交互的对象组成。相同种类的对象通常具有相同的类型,或者说,是在先沟通那个的class中。
例如,以Rectangle(矩形)对象为例,它具有length和width属性。根据设计,它可能需要接受这些属性值,计算面积和详细细节。
让我们来看看一个Rectangle类的实现,并借此套论C#的基本语法
1 using System;
2 namespace RectangleApplication
3 {
4 class Rectangle
5 {
6 //成员变量
7 double length;
8 double width;
9 public void Acceptdetails()
10 {
11 length = 4.5;
12 width = 3.5;
13
14 }
15 public double GetArea()
16 {
17 return length * width;
18
19 }
20 public void Display()
21 {
22 System.Console.WriteLine("Length:{0}", length);
23 System.Console.WriteLine("Width:{0}",width);
24 System.Console.WriteLine("Aree:{0}",GetArea());
25
26 }
27 class ExecuteRecatangle
28 {
29 static void Main(string[] args)
30 {
31 Rectangle r = new Rectangle();
32 r.Acceptdetails();
33 r.Display();
34 System.Console.ReadKey();
35 }
36 }
37 }
38 }
编译结果:
在任何C#程序中的第一条语句都是:
using System:
using 关键字用于在程序中包含命名空间,一个程序可以包含多个using语句。
class关键字用于声明一个类。
注释是用于解释代码。编译器会忽略注释的条目。在C#程序中,多行注释以/*开始,并以字符*/终止,
/*这是C#编程语言的多行注释
这是C#编程语言的多行注释
这是C#编程语言的多行注释*/
单行注释是用‘//’符号表示,
//这是C#语言的单行注释
变量是类的书香或数据成员,用于储存数据。在上面的程序中,Rectangle类有两个成员变量,名为length和width。
函数是一系列执行制定任务的语句,类的成员函数是在类内声明的。我们剧烈的泪Rectangle包含了三个成员函数:
AcceptDetails、GetArea、Display。
在上面的程序中,类ExecuteRectangle是一个包含Main()方法和实例化Rectangle类的类。
标识符是用来识别类、变量、函数或任何其他用户定义的项目。在C#中,类的命名必须遵循如下基本规则:
标识符必须以字幕或下划线(_)开头,后面可以跟一系列的字幕、数字或下划线。标识符中的第一个字符不能是数字。
标识符必须不含任何嵌入的空格或者符号,但是可以使用下划线。
标识符不能是C#关键字
关键字是C#编译器预定义的保留字,这些关键字不能有做标识符,但是,弱国你想使用这些关键字作为标识符,可以在关键字前面加上@字符作为前缀。
在c#中,有些标识符在代码的上下文中有特殊的意义,如get和set,这些成为上下位关键字。
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
add,alias,ascending,descending,dynamic,from,get,global,group,into,join,let,orderby,partial,remove,select,set