枚举enum
枚举的使用
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
namespace game_code
{
    enum E_MonsterType
    {
        Normal,// 普通怪
        Boos// Boos怪
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            // 枚举更多是用来标识某种状态,多和switch配合使用
            E_MonsterType Player = E_MonsterType.Normal;
            switch (Player) { 
                case E_MonsterType.Normal:
                    Console.WriteLine("执行普通怪的逻辑");
                    break; 
                case E_MonsterType.Boos:
                    Console.WriteLine("执行Boos怪的逻辑");
                    break;
                default:
                    break;
            } 
        }
    }
}
- 枚举一般在namespace语句块,class语句块,struct语句块中声明,
- 注意: 枚举不能在函数语句块中声明
枚举的类型转换
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
namespace game_code
{
    enum E_MonsterType
    {
        Normal,// 普通怪
        Boos// Boos怪
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            E_MonsterType PlayerType = E_MonsterType.Boos;
            // 枚举 -> int
            int a = (int)PlayerType;
            
            // 枚举 - > string
            string s = PlayerType.ToString();
            
            // int -> 枚举
            PlayerType = 0;// 普通怪
            // PlayerType = 1;// error
            
            // string -> 枚举
            PlayerType= (E_MonsterType)Enum.Parse(typeof(E_MonsterType), "Other");
        }
    }
}C#数组 && C++数组的区别

- C#数组是 int [] arr = {1,2,3,4,5};
- C++数组是 int arr[] = {1,2,3,4,5};
C#数组的使用 && 注意事项
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
namespace game_code
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] arr = {1,2,3,4,5};
            Console.WriteLine("arr的长度是{0}", arr.Length);
        }
    }
} 
- 一维数组.Length 得到数组长度
- 数组本身无法新增,或删除元素,当然如果必须的话,就必须要通过另一个数组实现
二维数组
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
namespace game_code
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region 二维数组
            int[,] arr;
            int[,] arr1 = new int[3, 3];
            int[,] arr2 = 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}
            };
            #endregion
            // 二维数组的遍历
            for(int i = 0;i < arr5.GetLength(0);i++)
            {
                for(int j = 0;j < arr5.GetLength(1); j++)
                {
                    Console.Write(arr5[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}
- 二维数组.GetLength(0) 表示二维数组的行
- 二维数组.GetLength(1) 表示二维数组的列
- 二维数组[i,i] 表示二维数组的i行j列的元素
交错数组(了解)
- 基本概念: 可以存储同一类型的m行不确定列的数据
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
namespace game_code
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int[][] arr1;
            int[][] arr2 = new int[3][];
            int[][] arr3 = new int[3][]
            {
                new int[] { 1, 2, 3 },
                new int[] { 1, 2 },
                new int[] { 1, },
            };
            int[][] arr4 = new int[][]
            {
                new int[] { 1, 2, 3 },
                new int[] { 1, 2 },
                new int[] { 1, },
            };
            int[][] arr5 =
            {
                new int[] { 1, 2, 3 },
                new int[] { 1, 2 },
                new int[] { 1, },
            };
            // 遍历交错数组
            for(int i = 0;i < arr5.GetLength(0); i++)
            {
                for(int j = 0;j < arr5[i].Length; j++)
                {
                    Console.Write(arr5[i][j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
} 
- 交错数组.GetLength(0) 得到行数
- 交错数组[i].Length 得到i行的列数
- 交错数组[i][j] 表示交错数组的i行j列的元素
引用类型 && 值类型
namespace game_code
{
    internal class Program
    {
        // 注意这里要加static
        static void Show(int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            int[] arr = { 1, 2, 3 };
            int[] arr1 = arr;
            for (int i = 0; i < arr1.Length; i++)
            {
                arr1[i] += 1;
            }
            Show(arr);
            Show(arr1);
            Console.WriteLine();
            int[] arr2 = arr1;
            arr2 = new int[] {1, 2, 3 };
            Show(arr1);
            Show(arr2);
        }
    }
} 
- 引用类型(本身就是引用): string 数组 类
- 值类型(值传递): 其他类型,结构体
特殊的string引用类型
namespace game_code
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string s = "123";
            string s1 = s;
            s1 = "234";
            Console.WriteLine(s);
            Console.WriteLine(s1);
        }
    }
}
- 虽然string是引用类型,但是它只表现出了值类型的特征
返回多个值的函数
namespace game_code
{
    internal class Program
    {
        static int [] Calc(int a,int b)
        {
            int sum = a + b;
            int avg = sum / 2;
            return new int[] {avg, sum};
        }
        static void Main(string[] args)
        {
            int a = Convert.ToInt32(Console.ReadLine());
            int b = Convert.ToInt32(Console.ReadLine());
            int []ret = Calc(a, b);
            Console.WriteLine(ret[0] + " " + ret[1]);
        }
    }
}
- 通过数组返回就可以实现返回多个值
引用ref && 引用out
namespace game_code
{
    internal class Program
    {
        static void ChangeValueRef(ref int value)
        {
            value = 10;// 可改可不改
        }
        static void ChangeValueRef(int value)
        {
            value = 10;// 可改可不改
        }
        static void ChangeArrayRef(ref int[] arr)
        {
            arr = new int[] {10,20,30 };// 可改可不改
        }
        static void ChangeValueOut(out int value)
        {
            value = 1000;// 必须改
        }
        static void ChangeArrayOut(out int[] arr)
        {
            arr = new int[] { 1000, 2000, 3000 };// 必须改
        }
        static void Show(int tmp)
        {
            Console.WriteLine(tmp);
        }
        static void Show(int[] tmp)
        {
           for(int i = 0; i < tmp.Length; i++) {
                Console.Write(tmp[i] + " ");
           }
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            // 用ref传的参数必须初始化
            int num = 2;
            int[] arr = new int[] {2,2,2 };
            Show(num);
            Show(arr);
            ChangeValueRef(ref num);
            ChangeArrayRef(ref arr);
            Show(num);
            Show(arr);
            // 用out传的参数不用初始化
            int num1;
            int[] arr1;
            ChangeValueOut(out num1);
            ChangeArrayOut(out arr1);
            Show(num1);
            Show(arr1);
        }
    }
}
-  ret传入的变量必须初始化 但是在内部 可改可不改 
-  out传入的变量不用初始化 但是在内部 必须修改该值(必须赋值) 
namespace game_code
{
    internal class Program
    {
        static void ChangeValueRef(ref int value)
        {
            value = 10;
        }
        static void ChangeValueRef(int value)
        {
            value = 10;
        }
        static void Main(string[] args)
        {
            // 用ref传的参数必须初始化
            int num = 2;
            Console.WriteLine(num);
            ChangeValueRef(num);
            Console.WriteLine("没加ret: {0}", num);
            ChangeValueRef(ref num);
            Console.WriteLine("加了ret: {0}", num);
        }
    }
} 
ref和out的作用: 解决值类型和引用类型 在函数内部 改值 或者 重新声明 能够影响外部传入的变量 让其也被修改
params变长参数
作用: 可以传入n个同类型参数,n可以是0
using Microsoft.VisualBasic;
namespace game_code
{
    internal class Program
    {
        static int Sum(params int[] nums)
        {
            int sum = 0;
            for(int i = 0;i < nums.Length;i++) { 
                sum += nums[i];
            }
            return sum;
        }
        static void Main(string[] args)
        {
            int[] arr = { 1, 2, 3, 4, 5, };
            int ret = Sum(arr);
            Console.WriteLine(ret);
        }
    }
} 
- params后面必须是数组,意味着只能是同一类型的可变参数
- 变长参数只能有一个
- 必须在所有参数的最后写变长参数
struct结构体 && 访问限定符
using Microsoft.VisualBasic;
namespace game_code
{
    internal class Program
    {
        // 结构体默认是private
        struct Person
        {
            // struct中的函数不用加static
            public Person(int age = 0,string name = "",int id = 0)
            {
                _age = age;
                _name = name;
                _id = id;
            }
            public int _age;
            public string _name;
            private int _id;// 学号
        }
        static void Main(string[] args)
        {
           Person A = new Person(18,"lyc最帅",1241241412);
           Console.WriteLine(A._name);
           // Console.WriteLine(A._id);// error
        }
    }
}
- public能在结构体内外访问,private只能在结构体外访问
- 在结构体中申明的变量 不能初始化 只能在外部或者在函数中赋值(构造函数)
- 在结构体中申明的函数 不用加static
冒泡排序

using Microsoft.VisualBasic;
namespace game_code
{
    internal class Program
    {
        static void Swap(ref int a,ref int b)
        {
            int tmp = a;
            a = b;
            b = tmp;
        } 
        static void Show(int[] arr)
        {
            for(int i = 0;i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            #region 冒泡排序
            int[] arr = { 9, 7, 1, 5, 4, 2, 6, 3, 8 };
            // int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            for (int i = 0;i < arr.Length;i++) { 
                bool isSwap = false;// 优化一
                for (int j = 1;j < arr.Length -i;j++){
                    // 前一个大于后一个就交换
                    if (arr[j-1] > arr[j]){
                        isSwap = true;
                        Swap(ref arr[j-1], ref arr[j]);
                    }
                }
                if (!isSwap){
                    break;
                }
            }
            Show(arr);
            #endregion
        }
    }
}
- 如果一轮比较下来都没有发生交换,则说明此时排序已经完成了
选项排序
using Microsoft.VisualBasic;
namespace game_code
{
    internal class Program
    {
        static void Swap(ref int a,ref int b)
        {
            int tmp = a;
            a = b;
            b = tmp;
        } 
        static void Show(int[] arr)
        {
            for(int i = 0;i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            #region 选择排序
            int[] arr1 = { 9, 7, 1, 5, 4, 2, 6, 3, 8 };
            for (int i = arr1.Length - 1; i >= 0; i--)
            {
                int tmp_pos = 0;// 中间商
                for (int j = 1; j < i + 1; j++)
                {
                    // 前一个大于后一个就交换
                    if (arr1[tmp_pos] < arr1[j])
                    {
                        tmp_pos = j;
                    }
                }
                Swap(ref arr1[tmp_pos], ref arr1[i]);
            }
            Show(arr1);
            #endregion
        }
    }
} 
- 新建中间商
- 依次比较
- 找出极值(最大或最小)
- 放入目标位置
优化版本

using Microsoft.VisualBasic;
namespace game_code
{
    internal class Program
    {
        static void Swap(ref int a,ref int b)
        {
            int tmp = a;
            a = b;
            b = tmp;
        } 
        static void Show(int[] arr)
        {
            for(int i = 0;i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            #region 选择排序(双中间商实现)
            int[] arr1 = { 1,9, 7, 1, 5, 4, 2, 6, 3, 8 ,0};
            int begin = 0;// 最左边
            int end = arr1.Length - 1;// 最右边
            while(begin < end)
            {
                int maxi = begin;// 找最大的值的下标
                int mini = begin;// 找最小的值的下标
                for(int i = begin; i <= end; i++)
                {
                    if (arr1[maxi] < arr1[i])
                    {
                        maxi = i;
                    }
                    if (arr1[mini] > arr1[i])
                    {
                        mini = i;
                    }
                }
                if(maxi == begin)
                {
                    Swap(ref arr1[begin++], ref arr1[mini]);
                    maxi = mini;
                }
                Swap(ref arr1[end--],ref arr1[maxi]);
            }
            Show(arr1);
            #endregion
        }
    }
}



















