1 枚举
|
enum E_MonsterType |
|
{ |
|
normal1, |
|
boss = 5, |
|
normal2, |
|
} |
|
|
|
E_MonsterType monsterType = E_MonsterType.boss;/声明枚举变量 |
|
switch (monsterType) |
|
{ |
|
case E_MonsterType.normal1: |
|
Console.WriteLine("普通怪物逻辑"); |
|
break; |
|
case E_MonsterType.boss: |
|
Console.WriteLine("boss怪物逻辑"); |
|
break; |
|
default: |
|
break; |
|
} |
2 数组
2.1 数组
|
int[] arr1; |
|
int[] arr2 = new int[5]; |
|
int[] arr3 = new int[5] {1,2,3,4,5}; |
|
int[] arr4 = new int[] {1,2}; |
|
int[] arr5 = {1,2,3,4,5,6,7}; |
|
int[] arr6 = new int[8]; |
|
bool[] arr7 = new bool[] { true, false }; |
|
|
|
Console.WriteLine(arr5.Length); |
|
foreach(int i in arr5) |
|
{ |
|
Console.WriteLine(i); |
|
} |
|
|
|
|
|
for(int i = 0; i < arr5.Length; i++) |
|
{ |
|
arr6[i] = arr5[i]; |
|
} |
|
arr6[7] = 8; |
2.2 二维数组
|
int[,] arr; |
|
int[,] arr2 = new int[3,3]; |
|
int[,] arr3 = new int[3, 3] { { 1, 2, 3 }, |
|
{ 4, 5, 6 }, |
|
{ 7, 8, 9 } }; |
|
int[,] arr4 = new int[, ] { { 1, 2, 3 },{ 4, 5, 6 },{ 7, 8, 9 } }; |
|
int[,] arr5 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; |
|
|
|
Console.WriteLine(arr5.GetLength(0)); |
|
Console.WriteLine(arr5.GetLength(1)); |
|
|
|
|
|
foreach(int i in arr5) |
|
{ |
|
Console.WriteLine("{0}",i); |
|
} |
2.3 交错数组
|
int[][] arr1 = new int[3][] { new int[] { 1, 2, 3 }, new int[] { 4, 5 }, new int[] { 6 } }; |
|
Console.WriteLine($"数组的行数为:{arr1.GetLength(0)}"); |
|
Console.WriteLine($"数组某一行的列数为:{arr1[0].Length}"); |
|
foreach (int[] a in arr1) |
|
{ |
|
foreach(int b in a) |
|
{ |
|
Console.WriteLine(b); |
|
} |
|
} |
3 值类型和引用类型
引用类型:string,数组,类 值类型:其它的,结构体
|
|
|
byte b1 = 1; |
|
ushort us = 1; |
|
uint ui = 1; |
|
ulong ul = 1; |
|
|
|
sbyte sb = 1; |
|
short s = 1; |
|
int i = 1; |
|
long l = 1; |
|
|
|
float f = 1f; |
|
double d = 1.1; |
|
decimal de = 1.1m; |
|
|
|
bool bo = true; |
|
char c = 'a'; |
|
string str = "strs"; |
|
|
|
|
|
|
|
|
|
|
值类型和引用类型区别
值类型 在相互赋值时 把内容复制给对方 它变我不变
引用类型在相互赋值时 让两者指向同一个值 它变我也变
|
|
|
|
|
int a = 10; |
|
|
|
int[] arr = { 1, 2, 3, 4, 5, 6, }; |
|
|
|
int b = a; |
|
int[] arr2 = arr; |
|
Console.WriteLine($"a={a},b={b}"); |
|
Console.WriteLine($"arr[0]={arr[0]},arr2[0]={arr2[0]}"); |
|
|
|
b = 20; |
|
arr2[0] = 0; |
|
Console.WriteLine("修改后:"); |
|
Console.WriteLine($"a={a},b={b}"); |
|
Console.WriteLine($"arr[0]={arr[0]},arr2[0]={arr2[0]}"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
arr2 = new int[] {99,1,2,3}; |
|
Console.WriteLine($"arr[0]={arr[0]},arr2[0]={arr2[0]}"); |
string的它变我不变
|
|
|
string str1 = "123"; |
|
string str2 = str1; |
|
|
|
|
|
|
|
str2 = "321"; |
4 函数
4.1 函数语法
|
|
|
static void SayHello() |
|
{ |
|
Console.WriteLine("你好!"); |
|
} |
|
|
|
static void SayYourName(string name) |
|
{ |
|
Console.WriteLine($"你的名字是:{name}"); |
|
} |
|
|
|
static string YourName() |
|
{ |
|
return "tyy"; |
|
} |
|
|
|
static int Sum(int a, int b) => a + b; |
|
|
|
|
|
static int[] SumAverage(int a, int b) |
|
{ |
|
int sum = a + b; |
|
int avg = sum / 2; |
|
|
|
return new int[] {sum,avg}; |
|
} |
|
|
|
SayHello(); |
|
SayYourName("tyy"); |
|
SayYourName(YourName()); |
|
Console.WriteLine(Sum(1,2)); |
|
int[] arr = SumAverage(7, 9); |
|
Console.WriteLine($"{ arr[0]}, {arr[1]}"); |
4.2 ref 和 out
作用:可以解决在函数内部改变外部传入的参数,函数里面改变外面也会改变
|
|
|
static void ChangeValue(int value) |
|
{ |
|
value = 3; |
|
} |
|
static void ChangeArray(int[] arr) |
|
{ |
|
arr = new int[] { 10, 20, 30 }; |
|
} |
|
static void ChangeArrayValue(int[] arr) |
|
{ |
|
arr[0] = 99; |
|
} |
|
|
|
|
|
static void ChangeValueRef(ref int value) |
|
{ |
|
value = 3; |
|
} |
|
static void ChangeArrayRef(ref int[] arr) |
|
{ |
|
arr = new int[] { 10, 20, 30 }; |
|
} |
|
|
|
|
|
static void ChangeValueOut(out int value) |
|
{ |
|
value = 33; |
|
} |
|
static void ChangeArrayOut(out int[] arr) |
|
{ |
|
arr = new int[] { 100, 20, 30 }; |
|
} |
|
|
|
int a = 1; |
|
ChangeValue(a); |
|
ChangeValueRef(ref a); |
|
ChangeValueOut(out a); |
|
|
|
int[] arr1 = {1, 2, 3 }; |
|
ChangeArray(arr1); |
|
ChangeArrayValue(arr1); |
|
ChangeArrayRef(ref arr1); |
|
ChangeArrayOut(out arr1); |
4.3 变长参数 params 和参数默认值
4.3.1 变长参数
params int[]意味着可以传入任意n个参数,n可以为0,传入的参数存在arr数组中
-
params 关键字后面一定是数组
-
数组可以是任意类型
-
函数的参数可以是 其他类型的参数和 params 修饰的参数
-
最多只能有一个 params 关键字,且一定放在最后一组参数
|
static int Sum(params int[] arr) |
|
{ |
|
int sum = 0; |
|
for(int i = 0; i < arr.Length; i++) |
|
{ |
|
sum += arr[i]; |
|
} |
|
return sum; |
|
} |
|
|
|
Sum(); |
|
Sum(1, 2, 3, 4, 1, 2, 4, 12, 5, 235, 5, 23, 1, 4, 1); |
4.3.2 参数默认值
当函数调用时如果不传入参数,则使用默认值作为参数传入 每个参数都可以有默认值,有默认值的参数必须写在普通参数的后面
|
static void Speak(string str = "没什么说的") |
|
{ |
|
Console.WriteLine(str); |
|
} |
|
Speak(); |
|
Speak("giao!"); |
4.4 函数重载
在同一语句块中,函数的名字相同,但参数的数量、类型或顺序不同,与返回值无关
|
static int Sum(int a, int b) |
|
{ |
|
return a + b; |
|
} |
|
|
|
static int Sum(int a, int b, int c) |
|
{ |
|
return a + b + c; |
|
} |
|
|
|
static float Sum(int a, float b) |
|
{ |
|
return a + b; |
|
} |
|
|
|
static float Sum(float a, int b) |
|
{ |
|
return a + b; |
|
} |
|
|
|
static int Sum(ref int a, int b) |
|
{ |
|
return a + b; |
|
} |
|
static int Sum(out int a, int b) |
|
{ |
|
a = 1; |
|
b = 2; |
|
return a + b; |
|
} |
|
|
|
static float Sum(int a, int b, params int[] arr) |
|
{ |
|
return 1; |
|
} |
5 结构体
|
struct Student |
|
{ |
|
|
|
|
|
|
|
|
|
public int age; |
|
public bool sex; |
|
public int number; |
|
public string name; |
|
|
|
|
|
public void Speak() |
|
{ |
|
|
|
Console.WriteLine($"我的名字是{name},今年{age}岁"); |
|
} |
|
|
|
|
|
|
|
public Student(int age, bool sex, int number, string name) |
|
{ |
|
|
|
this.age = age; |
|
this.sex = sex; |
|
this.number = number; |
|
this.name = name; |
|
} |
|
} |
|
|
|
Student s1; |
|
s1.age = 10; |
|
s1.sex = false; |
|
s1.number = 1; |
|
s1.name = "tyy"; |
|
s1.Speak(); |
出处:https://www.cnblogs.com/tanyuyang/p/17241990.html