一、前言

由于之前在用Unity3D开发直播弹幕小玩法,而U3D的脚本是C#语言。实不相瞒,在没有接触U3D前,我根本没用过C#,前面写的两篇DotNet入门也就是为了简单拓宽一下开发视野。

而这次就真的想学习一下C#语言基础,一是在用Unity3D开发弹幕游戏时更顺畅,二是方便以后直接用DotNet开发游戏服务端。

二、简易运行

平时用过Python、PHP或JS的,为了练习语法,喜欢在命令模式下运行单个文件。就比如我就写一个hello world的py文件,直接运行Python hello.py,不用在重型IDE下构建项目,随便用个文本编辑器就能练习。

所以,我把这一喜好用到了DotNet的练习上,尽管C#文件要便宜,但也可以类似上面的方法操作,前提是已经安装了DotNet。

1.查看版本

1
dotnet --version

2.创建工程

1
dotnet new console

由于是命令方式,就直接运行console模板,当然wpf也可以,运行后在当面目录下就会生成bin、obj文件夹和一个csproj文件,然后你可以在文件夹下创建一个比如hello.cs的C#文件。

3.编译运行

1
dotnet run hello.cs
1
dotnet run --project . --hello.cs

运行上面任一个命令就能马上编译并运行。

4.打包运行

1
dotnet build hello.cs  -o out  			 # 先编译到 out 目录 

5.运行编译文件

1
dotnet out/hello.dll           		# 然后运行编译后的 DLL

三、语法练习

其实从其他编程语言平移过来,最大的好处是不用从零开始学习新语言的基础。

主要区分一下数据类型,变量的声明与输出,其他的像逻辑控制,操作符,面向对象模式都是相同的,所以下面我就主要记录一些数据类型的使用。

1.基础类文件

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
namespace myAppliction;

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 Program
{

// 接收输入参数
static void Main(String[] args)
{
Rectangle r = new Rectangle();

r.Acceptdetails();

r.Display();
}

}

2.值类型

常用的几个值类型分别是bool(布尔)、double、float、int等类型,

1
2
3
4
5
6
7
8
public void TestDataVar()
{
int count = 89;

float new_data = 15.56f;

Console.WriteLine(new_data * count);
}

3.string类型

在C#中,字符串类型是属于引用类型,在堆上分配内存,声明时,要给一个默认值,不然编译时会抛出warn提示。

1
2
3
4
5
6
7
public string ?content = null;

public void WriteStrNullable()
{
content = "hello world";
Console.WriteLine(content);
}

4.枚举类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
enum Day 
{
Sun, Mon, Tue,
Wed, Thu, Fri,
Sat
}

public void outputEnum()
{
int x = (int)Day.Sun;
int y = (int)Day.Fri;
Console.WriteLine("Sun = {0}", x);
Console.WriteLine("Fri = {0}", y);

Console.WriteLine(Day.Sun);
}

5.结构类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 结构声明
struct MyStruct
{
public int X;
public int Y;

// 结构的构造函数需要有参数
public MyStruct(int x, int y)
{
X = x;
Y = y;
}
}

MyStruct structInstance1 = new MyStruct(1, 2);
structInstance1.X = 999;

Console.WriteLine(structInstance1.X);

6.数组类型

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
public void TestCycle()
{
string[] activitys = new string[] {
"活动一",
"活动二",
"活动三",
};

int i,count;

count = activitys.Length;

// for循环
for(i=0; i< count; i++)
{
Console.WriteLine(i + ":" + activitys[i]);
}

//foreach循环
foreach(string actName in activitys)
{
Console.WriteLine("活动名:" + actName);
}

Console.WriteLine(string.Join(",", activitys));
}

// 数组转字符串,打印数组
static void Main(String[] args)
{
Console.WriteLine(string.Join(",", args));
Console.Write(args[1]);
}

7.字典(泛集合类型)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void TestDict()
{
Dictionary<string, string> conuntriy = new Dictionary<string, string> {
{"ZH", "中国"},
{"USA", "美国"},
{"JP", "日本"}
};

foreach(var (key, values) in conuntriy)
{
Console.WriteLine("英文名:"+key+", 中文名:"+values);
}

foreach (var item in conuntriy)
{
Console.WriteLine("中文名"+item);
}

}

四、写在后面

C#有一点和其他解释型语言不同的是,string类型是引用类型,在堆上分配内存。一旦创建就不可更新,任何对字符串的修改操作都是在内存中进行新建。

image-20250408165643830