Csharp-01
开发环境以及 IDE 的使用
IDE:Visual Studio 2022
选择工作负荷:.NET 桌面开发
创建一个控制台应用程序
打开 Visual Studio 2022
选择“创建新项目”
选择“控制台应用程序 (.NET Framework)”
填写项目名称
点击“创建”
项目名称与解决方案的关系项目名称:项目的名称解决方案:项目的容器,一个解决方案可以包含多个项目
项目结构
Properties:项目属性
References:引用
Program.cs:程序入口
App.config:配置文件
Packages.config:NuGet 包管理
.vs:Visual Studio 配置文件
bin:编译后的文件
obj:编译过程中的文件
Hello World
1 | using System; |
基础结构
命名空间:namespace
类型:class、struct、interface、enum
函数、方法:static void Main(string[] args)
语句块:{}
语句:;
注释://、/* */
1 | // 最小结构 |
基础语法
注释
1 | // 单行注释 |
标识符
由字母、数字、下划线组成
不能以数字开头
不能是关键字
关键字
保留的关键字,不能用作标识符
保留的关键字
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 int interface internal 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 unchecked unsafe ushort using virtual void volatile while
变量
变量的声明
1
2
3
4
5
6
7
8// 声明变量
int a;
// 声明变量并赋值
int b = 1;
// 声明多个变量
int c, d, e;
// 声明多个变量并赋值
int f = 1, g = 2, h = 3;变量的命名规范
驼峰命名法
大驼峰:每个单词首字母大写
小驼峰:第一个单词首字母小写,其他单词首字母大写
命名规范
变量名要有意义
变量名不能是关键字
变量名不能以数字开头
变量名不能包含空格
变量名不能包含特殊字符
变量名不能包含中文
变量名不能包含下划线
变量的作用域
- 局部变量:在方法中声明的变量
- 成员变量:在类中声明的变量
变量的类型
- 值类型:存储在栈中,直接存储数据
- 整型:byte、short、int、long
- 浮点型:float、double
- 字符型:char
- 布尔型:bool
- 引用类型:存储在堆中,存储数据的地址
- 字符串:string
- 数组:int[]
变量的转换
隐式转换:小类型转换为大类型
显式转换:大类型转换为小类型
1
2
3
4
5
6// 隐式转换
int a = 1;
double b = a;
// 显式转换
double c = 1.1;
int d = (int)c;变量的默认值
类型 默认值 byte 0 short 0 int 0 long 0 float 0 double 0 char \0 bool false string null
常量
常量的声明
1
2
3
4// 声明常量
const int a = 1;
// 声明多个常量
const int b = 1, c = 2, d = 3;常量的命名规范
- 常量名要有意义
- 常量名不能是关键字
- 常量名不能以数字开头
- 常量名不能包含空格
- 常量名不能包含特殊字符
- 常量名不能包含中文
- 常量名不能包含下划线
常量的作用域
- 局部常量:在方法中声明的常量
- 成员常量:在类中声明的常量
运算符
算术运算符
- 加法:+
- 减法:-
- 乘法:*
- 除法:/
- 取余:%
- 自增:++
- 自减:–
赋值运算符
- 赋值:=
- 加法赋值:+=
- 减法赋值:-=
- 乘法赋值:*=
- 除法赋值:/=
- 取余赋值:%=
比较运算符
- 等于:==
- 不等于:!=
- 大于:>
- 小于:<
- 大于等于:>=
- 小于等于:<=
逻辑运算符
- 与:&&
- 或:||
- 非:!
位运算符
- 与:&
- 或:|
- 非:~
- 异或:^
- 左移:<<
- 右移:>>
条件运算符
三元运算符:?:
1
2
3int a = 1;
int b = 2;
int c = a > b ? a : b;
其他运算符
- is:判断类型
- as:类型转换
- sizeof:获取类型的大小
- typeof:获取类型
- default:获取类型的默认值
- checked:检查溢出
- unchecked:不检查溢出
流程控制
if 语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14int a = 1;
int b = 2;
if (a > b)
{
Console.WriteLine("a > b");
}
else if (a < b)
{
Console.WriteLine("a < b");
}
else
{
Console.WriteLine("a = b");
}switch 语句
1
2
3
4
5
6
7
8
9
10
11
12
13int a = 1;
switch (a)
{
case 1:
Console.WriteLine("a = 1");
break;
case 2:
Console.WriteLine("a = 2");
break;
default:
Console.WriteLine("a = 3");
break;
}while 语句
1
2
3
4
5
6int a = 1;
while (a < 10)
{
Console.WriteLine(a);
a++;
}do while 语句
1
2
3
4
5
6int a = 1;
do
{
Console.WriteLine(a);
a++;
} while (a < 10);for 语句
1
2
3
4for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}foreach 语句
1
2
3
4
5int[] arr = { 1, 2, 3, 4, 5 };
foreach (int i in arr)
{
Console.WriteLine(i);
}break 语句
1
2
3
4
5
6
7
8for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break;
}
Console.WriteLine(i);
}continue 语句
1
2
3
4
5
6
7
8for (int i = 0; i < 10; i++)
{
if (i == 5)
{
continue;
}
Console.WriteLine(i);
}goto 语句
1
2
3
4
5
6
7
8
9
10for (int i = 0; i < 10; i++)
{
if (i == 5
{
goto label;
}
Console.WriteLine(i);
}
label:
Console.WriteLine("goto");