运行环境

.Net + VStudio

公共语言运行时 CLR

简化开发,安全性,内存管理
简化应用程序部署
基类库
支持多种语言

编译和执行

c# 源文件 .cs 文件
-> 编译过程
元数据,中间代码 .exe 文件
-> 执行过程
即时编译(JIT) 本地代码,并执行

  • exe IL指令,元信息
  • 反编译工具 ildasm.exe -> NETFX.Tools

对象三要素

属性,方法,事件

常用应用程序类型

控制台程序
Windows应用程序(WPF,WinForm)
Web应用程序(ASP.NET)

数据类型

  • 数据类型本质是数据的“存储方式”及其能“参与的运算”的抽象
  • 值类型,引用类型

数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
Array.Copy(source, 0, dest, 0, source.Length);
int [,] a = {{},{},{}}
a.GetLength(0), a.GetLength(1)
int[][] t = new int[3][];

/* 示例 */
const int N = 4; //按钮的行、列数
Button[,] buttons = new Button[N, N]; //按钮的数组
private void Form1_Load(object sender, EventArgs e)
{
//产生所有按钮
GenerateAllButtons();
}
private void button1_Click(object sender, EventArgs e)
{
//打乱顺序
Shuffle();
}
//打乱顺序
void Shuffle()
{
//多次随机交换两个按钮
Random rnd = new Random();
for (int i = 0; i < 100; i++)
{
int a = rnd.Next(N);
int b = rnd.Next(N);
int c = rnd.Next(N);
int d = rnd.Next(N);
Swap(buttons[a, b], buttons[c, d]);
}
}
//生成所有的按钮
void GenerateAllButtons()
{
int x0 = 100, y0 = 10, w = 45, d = 50;
for (int r = 0; r < N; r++)
for (int c = 0; c < N; c++)
{
int num = r * N + c;
Button btn = new Button();
btn.Text = (num + 1).ToString();
btn.Top = y0 + r * d;
btn.Left = x0 + c * d;
btn.Width = w;
btn.Height = w;
btn.Visible = true;
btn.Tag = r * N + c; //这个数据用来表示它所在行列位置

//注册事件
btn.Click += new EventHandler(btn_Click);

buttons[r, c] = btn; //放到数组中
this.Controls.Add(btn); //加到界面上
}
buttons[N - 1, N - 1].Visible = false; //最后一个不可见
}
//交换两个按钮
void Swap(Button btna, Button btnb)
{
string t = btna.Text;
btna.Text = btnb.Text;
btnb.Text = t;

bool v = btna.Visible;
btna.Visible = btnb.Visible;
btnb.Visible = v;
}
//按钮点击事件处理
void btn_Click(object sender, EventArgs e)
{
Button btn = sender as Button; //当前点中的按钮
Button blank = FindHiddenButton(); //空白按钮
//判断是否与空白块相邻,如果是,则交换
if (IsNeighbor(btn, blank))
{
Swap(btn, blank);
blank.Focus();
}
//判断是否完成了
if (ResultIsOk())
{
MessageBox.Show("ok");
}
}
//查找要隐藏的按钮
Button FindHiddenButton()
{
for (int r = 0; r < N; r++)
for (int c = 0; c < N; c++)
{
if (!buttons[r, c].Visible)
{
return buttons[r, c];
}
}
return null;
}
//判断是否相邻
bool IsNeighbor(Button btnA, Button btnB)
{
int a = (int)btnA.Tag; //Tag中记录是行列位置
int b = (int)btnB.Tag;
int r1 = a / N, c1 = a % N;
int r2 = b / N, c2 = b % N;

if (r1 == r2 && (c1 == c2 - 1 || c1 == c2 + 1) //左右相邻
|| c1 == c2 && (r1 == r2 - 1 || r1 == r2 + 1))
return true;
return false;
}
//检查是否完成
bool ResultIsOk()
{
for (int r = 0; r < N; r++)
for (int c = 0; c < N; c++)
{
if (buttons[r, c].Text != (r * N + c + 1).ToString())
{
return false;
}
}
return true;
}

类 Class

基本要素:变量,函数

构造方法

作用:初始化值
方法名和类名相同
没有返回值,也不能写
构造方法不能显示直接调用,可以 new 调用

同名方法的重载

this

对象本身,访问对象字段和方法
区分字段和局部变量
构造方法调用另一个构造方法

1
public Person() :this(0,""){ ... }

属性==方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
private string _name;
public string Name {
get{ return _name;}
set{ _name = value;}
}
public string Name {set; get;}

///索引器
public string this[参数列表] {set; get;}
/*示例*/
using System;
class IndexerRecord
{
private string [] data = new string [6];
private string [] keys = {
"Author", "Publisher", "Title",
"Subject", "ISBN", "Comments"
};

//注:程序中用了两种方法来索引:
//一是整数作下标,二是字符串(关键词名)作下标
public string this[ int idx ]
{
set
{
if( idx >= 0 && idx < data.Length )
data[ idx ] = value;
}
get
{
if( idx >= 0 && idx < data.Length )
return data[ idx ];
return null;
}
}
public string this[ string key ]
{
set
{
int idx = FindKey( key );
this[ idx ] = value;
}
get
{
return this[ FindKey(key) ];
}
}
private int FindKey( string key )
{
for( int i=0; i<keys.Length; i++)
if( keys[i] == key ) return i;
return -1;
}
static void Main()
{
IndexerRecord record = new IndexerRecord();
record[ 0 ] = "马克-吐温";
record[ 1 ] = "Crox出版公司";
record[ 2 ] = "汤姆-索亚历险记";
Console.WriteLine( record[ "Title" ] );
Console.WriteLine( record[ "Author" ] );
Console.WriteLine( record[ "Publisher" ] );
}
}

设置只读,只写;
进行有效性检查;
计算数据返回;
定义抽象属性;