C# 是一种面向对象的编程语言。
在面向对象的程序设计方法中,程序由各种相互交互的对象组成。
相同种类的对象通常具有相同的类型,或者说,是在相同的 class 中。
例如,以 Rectangle(矩形)对象为例。它具有 length 和 width 属性。
根据设计,它可能需要接受这些属性值、计算面积和显示细节。
让我们来看看一个 Rectangle(矩形)类的实现,并借此讨论 C# 的基本语法:
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();
}
}
}
//-------------输出内容------//
//---Length: 4.5
//---Width: 3.5
//---Area: 15.75
C# 基本语法
3.2.1 using关键字
在任何 C# 程序中的第一条语句都是:
using System;
一般在程序开头添加 using System;,这时System.String 就可简写为string 。
例如:
// using System;//----这时候不添加 using System
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
System.String a = "Hello World!";
System.Console.WriteLine(bbb);
System.Console.ReadKey();
}
}
}
和
using System; //----添加 using System
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string a = "Hello World!"; //---System.String 中System可以省略
Console.WriteLine(a);//---System.Console中System可以省略
Console.ReadKey();//---System.Console中System可以省略
}
}
}
是等价的。
using 关键字用于在程序中包含命名空间。一个程序可以包含多个 using 语句。
class关键字用于声明一个类
注释是用于解释代码。
编译器会忽略注释的条目,多行注释以/*开始,并以字符 */ 终止,单行注释是用 ‘//’ 符号表示;
成员变量是类的属性或数据成员,用于存储数据。在上面的程序中,Rectangle 类有两个成员变量,名为 length 和 width。
实例化一个类,类 ExecuteRectangle 是一个包含 Main() 方法和实例化 Rectangle 类的类。
标识符
标识符是用来识别类、变量、函数或任何其它用户定义的项目。
在 C# 中,类的命名必须遵循如下基本规则:
—标识符必须以字母、下划线或 @ 开头,后面可以跟一系列的字母、数字( 0 - 9 )、下划线( _ )、@。
— 标识符中的第一个字符不能是数字。
—标识符必须不包含任何嵌入的空格或符号,比如 ? - +! # % ^ & * ( ) [ ] { } . ; : " ’ / \。
—标识符不能是 C# 关键字。除非它们有一个 @ 前缀。 例如,@if 是有效的标识符,但 if 不是,因为 if 是关键字。
—标识符必须区分大小写。大写字母和小写字母被认为是不同的字母。
—不能与C#的类库名称相同。
C# 关键字
关键字是 C# 编译器预定义的保留字。这些关键字不能用作标识符,但是,如果您想使用这些关键字作为标识符,可以在关键字前面加上 @ 字符作为前缀。
在 C# 中,有些关键字在代码的上下文中有特殊的意义,如 get 和 set,这些被称为上下文关键字(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/extern/fasle/finally/fixed/float/for
foreach/goto/if/implicit/in/in(generic modifiler)/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/unshort/using/virtual/void
volatile/while
上下文关键字(Contextual Keywords)
add/alias/ascending/descending/dynamic/from/get
global/group/into/join/let/orderby/partial
partial(method)/remove/select/srt…
C# 占位符{}
当 WriteLine() 函数有多个参数时,输出第一个参数(双引号内的)中的内容,而第二个及后面的参数中的内容替换掉第一个参数中对应位置的占位符一起输出。
static void Main(string[] args)
{
Console.WriteLine("A:{0},a:{1}",65,97);
Console.ReadLine();
}
//---------- 输出内容为 A:65,a:97 ------------
如果第一个参数没有留占位符,那么第二个参数内容不输出
Console.WriteLine("A:,a:",65,97);
//---------- 输出内容为 A:,a: ------------
占位符从零开始计数,且占位符中的数字不能大于第二个及后面的参数的总个数减一(要求占位符必须有可替换的值)。
占位符数字与第二个及后面的参数字符位置一一对应。
static void Main(string[] args)
{
Console.WriteLine("A:{1},a:{0}",65,97);
Console.ReadLine();
}
//--------------- 输出内容为 A:97,a:65 ---------------