C# 基本程序结构
最基本的C#代码包含以下结构:
- 命名空间声明(Namespace declaration)
- class
- Class方法
- Class属性
- Main方法
- 语句(Statements)&表达式(Expressions)
- 注释
具体结构示例如下:
using System
namespace HelloWorldApplication // 1.命名空间声明
{
class HelloWorld // 2.class
{
static void Main(String[] args) // 5.Main方法
{
Console.WriteLine("Hello World!"); // 6.语句&表达式
Console.ReadKey();
}
}
}
和其他语言一样,C#的 Main()
方法只能有一个,是程序的
在C#的语言基础之中,需要注意的是:
- C#的大小写敏感
- 语句以
;
结尾 - 程序从
Main()
开始执行 - 文件名可以不同于类名称(与Java区别)
C# 基本语法
将C#的编程方法完全看成是面向对象的——即尽量用相互交互的对象组成。相同种类的对象具有相同的类型,即在相同的 class
当中。
举例,设计一个矩形(Rectangle)对象:有长(length)、宽(width)属性。除此之外,还要有计算面积的方法。
using System // 所有C#的第一条语句,用于包含命名空间
namespace RectangleApplication
{
class Rectangle // 声明 Rectangle 类
{
double length; // 声明 length, width 两个属性,也叫成员变量
double width;
public void AcceptDetails() // 成员函数1,接入具体的长宽数值
{
length = 4.5;
width = 3.5;
}
public double GetArea() // 成员函数2,计算面积
{
return length*width;
}
public void Display() // 成员函数3,展示结果
{
Console.WriteLine("长 : {0}", length);
Console.WriteLine("宽 : {0}", width);
Console.WriteLine("面积 : {0}", GetArea());
}
}
class ExecuteRectangle // 声明第二个类,该类用于主程序入口
{
static void Main(string[] args) // Main 主程序入口
{
Rectangle x = new Rectangle(); // 将第一个类实例化
x.AcceptDetails(); // 接入具体长宽的值
x.Display(); // 展示结果
Console.ReadLine(); // 保持窗口
}
}
}
再次分析这组代码,其结构是这样子的:
- 一个命名空间
RectangleApplication
,包含了Rectangle
和ExecuteRectangle
两个类 Rectangle
类用于定义矩形,包含- length、width 两个成员变量(属性)
- AcceptDetails、GetArea、Display 三个成员函数(方法)
ExecuteRectangle
类用于执行主程序,是程序的入口- 将 Rectangle类 实例化为 x
- 执行 x 的两个成员函数
关于成员函数定义
成员函数定义总是形如下面:
public void Miao() // 公有的,实例化后在所有的类中都可以进行调用
{
...
}
private void MiaoMiao() // 私有的,只能在本类中调用,其他类不可见
{
...
}
static void Main() // 静态的,一般用于主函数,不用实例化就可以调用
{
...
}
类的实例化
在实例中,对Rectangle进行实例化的操作为:
Rectangle x = new Rectangle();
变量 x
因此成为 Rectangle类 的实例。
注释
C#的单行、多行注释如下:
// 单行注释
/*
多行
注释
*/
和C++是一样的。
C# 的标识符
标识符就是对类、函数、变量或者更多自定义项目的命名。C#的标识符命名原则如下:
- 字母 / _ / @ 开头,不可以数字开头
- 不能包含空格或者符号
- 不可以是关键字,可以用 @+关键字 组成特殊的,如 @if
- 区分大小写
- 避免与C#的类库名称相同
C# 关键字
C#的关键字分为保留关键字(Reserved Keywords)和上下文关键字(Contextual 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 | 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 |
上下文关键字 | ||||||
---|---|---|---|---|---|---|
add | alias | ascending | descending | dynamic | from | get |
global | group | into | join | let | orderby | partial(type) |
partial(method) | remove | select | set |
评论