C#基础

92 阅读8分钟

1、变量

namespace variable
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //变量一共有14种
            //有符号整形变量能存储正负值
            sbyte sb = 1;//范围是-128~127
            int i = 2;//-21亿~21亿
            short s = 3;//-32768~32767
            long l = 4;//-900万兆~900万兆
            //无符号整形变量,能存储0和正数
            byte b = 1;//0~255
            uint ui = 2;//0~42亿
            ushort us = 3;//0~65535
            ulong ul = 4;//0~1800w兆
            //浮点数
            float f = 1.2345678910f;//7、8位有效数字
            Console.WriteLine(f);
            double d = 1.34;//默认小数的类型,15~17
            decimal de = 1.45m;//27~28
            //特殊类型
            bool bo = true;//两种true和false
            char c = 'c';//存储单个字符
            string str = "1234";//字符串
            #region
            //不同数据类型占用内存空间不同
            //声明多个类型
            int a = 1, b1 = 2;
            #endregion
            #region 变量存储空间
            //1 byte = 8bit
            int sbyteSize = sizeof(sbyte);//使用sizeof()方法来打印变量所占大小
            Console.Write("sbyte所占字节数为:" + sbyteSize);
            #endregion
        }
    }
}

常量和转义字符

namespace ConstAndEscapeCharacter
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //常量需要初始化和const来定义并且不可修改
            const int a = 10;
            //\t制表符,\n换行等
            Console.WriteLine("\ta");
        }
    }
}

条件运算符

namespace 条件运算符
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region 条件运算符
            //用于比较两个变量和常量。
            //有> < >= <= == !=
            //条件运算符优先级低于算数运算符
            #endregion
            #region 不同类型比较
            //数值之间可以直接比较
            bool b1 = 12.43 < 12;
            #endregion
            char a = 'A';
            bool b2 = a < 'B';
            //其他类型如bool和string只能== 和 != 比较
        }
    }
}

Switch语句

namespace Switch语句
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string count = "0";
            #region 基础语法
            switch (count)
            {
                case "0":
                    Console.WriteLine("你输入了0");
                    break;
                case "1":
                    Console.WriteLine("你输入了1");
                    break;
                default:
                    Console.WriteLine("你输入了其他数字");
                    break;
            }
            //switch只能判断一个值,不能判断范围和运算。
            #endregion
            //贯穿:不写执行语句和break就叫贯穿
            switch (count)
            {
                case "0":
                case "1":
                    Console.WriteLine("你输入了1");
                    break;
                default:
                    Console.WriteLine("你输入了其他数字");
                    break;
            }
        }
    }
}

三大循环语句

namespace 循环语句
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region while语句
            //bool类型的语句 条件类型变量 逻辑运算符
            int i = 1;
            while (i < 10)
            {
                Console.WriteLine("ni" + i);
                i++;
                //continue; 回到循环开始
                //break; break 跳出循环
                //在Switch中break会起对switch起作用,而continue只对循环起作用
            }
            #endregion
 
            #region do...while循环
            do
            {
                Console.WriteLine("123");
            } while (false);
            #endregion
 
            #region for循环
            //for(初始表达式; 条件表达式; 增量表达式){}
            for(int num = 0; num < 10; num++)
            {
                Console.WriteLine("for循环打印i" + num);
            }
            #endregion
        }
    }
}

# 枚举

namespace 枚举
{           
    //一般在namespace中声明,不能在函数中声明
    #region 枚举基础概念
    //被命名的整形常量集合,一般用来表达状态和类型等等。
    #endregion
    #region 声明枚举和声明枚举变量
    //枚举名一般以E开头或以E_开头作为命名规范
    enum E_name
    {
        //默认第一个包裹的整形为0,递增+1
        自定义1,//0
        自定义2 = 4,
        自定义3,//默认+1为5
    }
    #endregion
    internal class Program
    {
        static void Main(string[] args)
        {
            #region 枚举的使用
            E_name e_Name = 0;
            if (e_Name == E_name.自定义1) Console.WriteLine("自定义1");
 
            //与switch
            switch (e_Name)
            {
                case E_name.自定义1:
                    Console.WriteLine("111");
                    break;
                case E_name.自定义2:
                    break;
                case E_name.自定义3:
                    break;
                default:
                    break;
            }
            #endregion
 
            #region 枚举的类型转换
            Console.WriteLine((int)E_name.自定义3);//转int类
            Console.WriteLine(e_Name.ToString());//转string
            #endregion
 
        }
    }
}

# 数组

namespace 数组
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region 基本概念
            //相同元素的集合,有一维、多维、交错数组等
            #endregion
            #region 数组的声明
            int[] ints1;
            int[] ints2 = new int[4];//声明有四个位置的数组,默认为0
            int[] ints3 = new int[4] { 1, 2, 3, 4 };
            int[] ints4 = new int[] { 1, 2, 3, 4 };
            int[] ints5 = { };
            #endregion
            #region 数组的使用
            int[] ints6 = { 1, 2, 3, 4, 5, 6 };
            //数组长度
            Console.WriteLine(ints6.Length);
            //获取元素
            Console.WriteLine(ints6[0]);
            #endregion
        }
    }
}
#region 二维数组
//声明
int[,] arr1;
int[,] arr2 = new int[1, 2];
int[,] arr3 = new int[2, 2] { { 1, 2 }, 
                              { 1, 2 } };
int[,] arr4 = new int[,] { { 1, 2 },
                          { 1, 2 } };
int[,] arr5 ={ { 1, 2 },
               { 1, 2 } };
//多维数组长度
Console.WriteLine(arr5.GetLength(0));//得到行
Console.WriteLine(arr5.GetLength(1));//得到列
//获取元素
Console.WriteLine(arr5[0, 1]);
#endregion
#region 交错数组
//数组的数组,每个维度不同
//交错的声明
int[][] a1;
int[][] a2 = new int[3][];
int[][] a3 = new int[3][] { new int[]{1, 2, 3, 4},
                            new int[]{1, 2, 3},
                            new int[]{1, 2},};
//交错数组长度获取
Console.WriteLine(a3.GetLength(0));//获取行数
Console.WriteLine(a3[0].Length);//获取第1行的列数
//遍历交错数组
for(int i = 0; i < a3.GetLength(0); i++)
{
    for(int j = 0; j < a3[i].Length; j++)
    {
        Console.Write(a3[i][j] + " ");
    }
    Console.WriteLine();
}
#endregion

# 值类型和引用类型

namespace 值类型和引用类型
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //无符号整型
            byte byte1 = 1;
            ushort ushort1 = 1;
            uint uint1 = 1;
            ulong ulong1 = 1;
            //有符号整型
            sbyte ubyte1 = 1;
            short short1 = 1;
            int int1 = 1;
            long long1 = 1;
            //浮点数
            float float1 = 1f;
            double double1 = 1d;
            decimal decimal1 = 1m;
            //其他类型
            bool bool1 = true;
            char char1 = 'A';
            string string1 = "string111";
            //复杂类型
            //枚举enum
            //数组(一维,多维)
 
            //引用类型:string、数组、类
            //值类型:其他、结构体
 
            //值类型 -- 栈内存 -- 系统分配,自动回收,小、快
            //引用类型 -- 堆内存 -- 手动申请和释放,大、慢
 
            #region 特殊引用类型
            //string的它变我不变
            string str1 = "123";
            string str2 = str1;
            str1 = "321";
            Console.WriteLine("str1:{0}, str2:{1}", str1, str2);//>str1:321, str2:123
                                                                //string类型重新赋值就相当于new了,或者说是换了一个地址。
            #endregion
            #region 监测地址
            //处于调试状态时,在调试(D)中窗口开启监视。
            #endregion
 
        }
    }
}

# 函数

namespace 函数
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region 基础概念
            //一块代码取了名字,可以用名字来使用
            //写在1、class里   2、struct语句块中
            //基本写法:1       2      3                4
            //       static 返回类型 函数名(参数类型 参数名1, 参数类型 参数名2, ...)
            //      {
            //          语句;
            //          return 返回值;(返回类型不是void才有。返回类型可以是类、class、struct、枚举enum、数组和其他14中值类型)
            //      }
            Console.WriteLine();
            #endregion
            #region ref 和 out
            //解决函数内部改变函数外部传入的内容
            int num = 1;
            static void ChangeValueRef(ref int value)
            {
                value = 2;
            }
            ChangeValueRef(ref num);
            Console.WriteLine(num);
            //ref传入的变量必须初始化
            //out必须在内部进行赋值。
            #endregion
        }
    }
}

# 变长参数和参数默认值

namespace 变长参数和参数默认值
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //方法声明时为形参,方法调用时为实参。
            #region 变长参数
            //使用params关键字,然后写对应类型数组和数组名
            static void fun1(params int[] arr)
            {
                for(int i = 0; i < arr.Length; i++)
                {
                    Console.Write(arr[i] + " ");
                }
            }
            fun1(1, 2, 5);
            //函数参数还可以有别的参数和params参数。但是params参数只能在最后一组,前面可以有多个其他参数
            static void fun2(int num1, string str1, params int[] arr)
            {
                for (int i = 0; i < arr.Length; i++)
                {
                    Console.Write(arr[i] + " ");
                }
            }
            fun2(1, "123", 1, 2, 3);
            #endregion
 
            #region 可选参数(有默认值的参数)
            //支持多参数,可选参数在普通参数后
            static void fun3(string str1 = "无话可说", params int[] arr)
            {
                for (int i = 0; i < arr.Length; i++)
                {
                    Console.Write(arr[i] + " ");
                }
                Console.WriteLine(str1);
            }
            fun3("123", 1, 2, 3);
            #endregion
        }
    }
}

# 方法重载

namespace 方法重载
{
    internal class Program
    {
        #region 方法重载
        //方法的名字相同,但是参数的数量或者类型和顺序不同
        //方法重载的要求
        //方法重载的合法条件是:相同的方法名,但 参数类型、数量或顺序不同。所有重载方法必须 定义在同一个类作用域内(如 class Program)。
        //ref和out这两可以看做是变量类型变了,但这两同样不能用在一模一样的方法中
        //可选参数也是看做新的变量类型。
        //方法重载和返回值无关。
        static int fun1(int a, int b)
        {
            Console.WriteLine(a + " " + b);
            return a + b;
        }
        static float fun1(float a, float b)
        {
            Console.WriteLine(a + " " + b);
            return a + b;
        }
        
        #endregion
        static void Main(string[] args)
        {
            fun1(1, 2);
            fun1(1f, 2f);
        }
    }
}

# 递归

namespace 递归函数
{
    internal class Program
    {
        static int i = 1;
        #region 递归
        static void fun1()
        {
            //第四部结束语句
            if(i > 10)
            {
                return;
            }
            //第二步语句书写
            Console.WriteLine("打印了:" + i);
            //第三步递归条件变化
            i++;
            //第一制造递归
            fun1();
        }
        #endregion
        static void Main(string[] args)
        {
            fun1();
        }
    }
}

# 结构体

namespace 结构体
{
    #region 本段代码目前需要的修饰符
    //默认是私有的(provate)
    //public 公共的
    #endregion
 
    #region 基本概念
    //结构体是有关系数据的集合,比如学生,动物等。
    //结构体一般在namespace中定义,使用struct关键字。
    //可以声明方法和变量
    #endregion
 
    #region 基础语法
    struct TempStruct
    {
        //第一部分变量,结构体变量不能直接初始化,变量类型任意除了自己
        public string name;
        public int atk;
        public int def;
        //第二部分构造函数
        public TempStruct(string name)
        {
            this.name = name;
        }
        //第三部分函数
        public void cut()
        {
            Console.WriteLine("{0}使用砍,攻击概率为:{1}", name, atk);
        }
    }
    #endregion
    internal class Program
    {
        static void Main(string[] args)
        {
            #region 结构体使用
            //类型 变量名
            TempStruct t;
            t.name = "战士";
            t.atk = 10;
            t.def = 3;
            t.cut();
 
            TempStruct t1 = new TempStruct("123");
            t1.cut();
            #endregion
        }
    }
}

# 算法-排序

namespace 冒泡排序
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //自己写的冒泡排序
            int[] arr = {3, 43, 12, 51, 11, 11, 21, 1};
            int temp;
            for (int j = 0; j < arr.Length - 1; j++)
            {
                for (int i = 0; i < arr.Length - 1 - j; i++)
                {
                    if (arr[i] > arr[i + 1])
                    {
                        temp = arr[i + 1];
                        arr[i + 1] = arr[i];
                        arr[i] = temp;
                    }
                }
            }
            
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }
        }
    }
}
#region 选择排序
int[] arr1 = { 3, 43, 12, 51, 11, 11, 21, 1 };
int index;
int temp1;
for(int i = 0; i < arr1.Length - 1; i++)
{
    index = 0;
    int len = arr1.Length - 1 - i;
    for(int j = 0; j < len; j++)
    {
        if(arr1[index] < arr1[j])
        {
            index = j;
        }
    }
    temp = arr1[len];
    arr1[len] = arr1[index];
    arr1[index] = temp;
}
Console.WriteLine("选择排序:");
for (int i = 0; i < arr.Length; i++)
{
    Console.Write(arr[i] + " ");
}
#endregion