美文网首页C#.NET
C#语法基础

C#语法基础

作者: DouDouZH | 来源:发表于2018-11-21 19:48 被阅读0次

    一、C#中的注释

    不写注释是流氓
    名字瞎起是扯淡

    1、注释符作用
    • 注释:解释说明代码的作用
    • 注销:注销的代码不会被执行
    2、C#的三种注释
    • 单行注释 //
    • 多行注释 /**/
    • 文档注释 /// 多用来解释类或方法

    二、vs中常用的快捷键

    • crl+k+d:快速对其代码
    • ctrl+z:撤销
    • ctrl+s:保存
    • ctrl+j:弹出智能提示
    • ctrl+k+c注释所选代码
    • ctrl+k+u取消对所选代码的注释
    • f1:转到帮助人当
    • 啊#Region和#EndRegion:折叠冗余代码

    三、变量

    1、变量的存储
    变量和地址
    变量的命名
    2、变量的类型

    语法:变量类型 变量名=值

    • int
    • double
    • string
    • char
    • decimal:金钱类型,后面要加m,decimal m=500m

    四、运算符

    • =:赋值运算符
    • +:连接两个字符串,或者做运算
    • -:减法运算
    • /:除法运算
    • *:乘法运算
    • %:取余数

    五、类型转换

    //隐式转换
    int num=100;
    double d=num;
    
    //强制转换
    double d=1.1;
    int i=(int)d;
    

    六、异常捕获

    try{
        //可能出现异常的代码
    }catch{
        //出现异常后执行的代码
    }
    

    七、枚举

    枚举就是一种变量类型
    语法

    [public] enum 枚举名{
      值1,值2,值3,······
    }
    
    • public:修饰符
    • enum:关键字,声明枚举

    小实例

    using System;
    
    namespace day_01_1
    {
        //将枚举声明到命名空间下面,类上面,这个命名空间所有类都可以使用
        //枚举就是一种变量类型
        public enum Gender {
            男,
            女,
        }
        class Program
        {
            static void Main(string[] args)
            {
                //使用枚举
                Gender gender = Gender.女;
                Console.WriteLine(gender);
                Console.ReadKey();
            }
        }
    }
    
    运行结果

    八、枚举和int 和String之间的转换

    1、枚举转换成int和string
    using System;
    
    namespace day_01_1
    {
        //将枚举声明到命名空间下面,类上面,这个命名空间所有类都可以使用
        //枚举就是一种变量类型
        public enum QQState {
            OnLine,
            OffLine,
            Leave,
            Busy,
            QMe,
        }
        class Program
        {
            static void Main(string[] args)
            {
                //使用枚举   枚举默认从0开始 这样输出为1
                //枚举转换成int
                QQState state = QQState.OffLine;
                int n = (int)state;
                Console.WriteLine(n);
                //枚举转换成string
                string n2 = state.ToString();
                Console.WriteLine(n2);
                Console.ReadKey();
    
            }
        }
    }
    
    
    运行结果
    2、int和string转换成枚举
    using System;
    
    namespace day_01_1
    {
        //将枚举声明到命名空间下面,类上面,这个命名空间所有类都可以使用
        //枚举就是一种变量类型
        public enum QQState {
            OnLine,
            OffLine,
            Leave,
            Busy,
            QMe,
        }
        class Program
        {
            static void Main(string[] args)
            {
    
    
                //将s转换成枚举类型
                //转换成int的方法Convert.ToInt32() interesting.parse() int.TryParse()
                string s = "0";
                //调用Parse()目的就是将字符串转换成对应的枚举类型
                //转换不了抛异常
                QQState state1= (QQState)Enum.Parse(typeof(QQState),s);
                Console.WriteLine(state1);
                Console.ReadKey();
            }
        }
    }
    
    运行结果

    九、结构

    一次帮我们生成多个变量
    语法

    public struct 结构名
    {
        成员;//变量
    }
    

    例子

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace day01_2
    {
        //声明结构
        public struct Person {
            public string name;
            public int age;
            public char gender;
            public int grade;
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                //xx大学管理系统
                //姓名,性别,年龄,年级
    
                //string zsName = "张三";
                //int zsAge = 21;
                //char zsGender = '男';
                //int zsGrade = 3;
    
                //声明结构变量
                Person zsPerson;
                zsPerson.age = 21;
                zsPerson.name = "张三";
                zsPerson.gender = '男';
                zsPerson.grade = 3;
                Console.WriteLine("姓名:{0}\n年龄:{1}\n性别:{2}\n班级:{3}",zsPerson.name,zsPerson.age,zsPerson.gender,zsPerson.grade);
                Console.ReadKey();
            }
        }
    }
    
    
    运行结果

    十、方法的三个高级参数和重载、递归

    1、out

    写一个方法求一个数组中的最大值,最小值,总和、平均值,返回多个相同类型的值可以用数组,如果返回多个不同类型的值可以使用out

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //写一个方法求一个数组中的最大值,最小值,总和、平均值
                int[] num = {1,2,3,4,5,6,7,8,9 };
                //将值放在数组中
                Console.WriteLine("返回数组求值:");
                int[] res = getMaxMinSumAvg(num);
                Console.WriteLine("最大值{0},最小值{1},总和{2},平均值{3}", res[0],res[1],res[2],res[3]);
                //out返回值
                int max = 0;
                int min = 0;
                int sum = 0;
                int avg = 0;
                Test(num, out max, out min, out sum, out avg);
                Console.WriteLine("out返回值:");
                Console.WriteLine("最大值{0},最小值{1},总和{2},平均值{3}", max, min, sum, avg);
                Console.ReadKey();
            }
            /// <summary>
            /// 返回数组方法求一个数组中的最大值,最小值,总和、平均值
            /// </summary>
            /// <param name="nums"></param>
            /// <returns></returns>
            public static int[] getMaxMinSumAvg(int [] nums) {
                int[] res = new int[4];
                //假设res[0]最大值 res[1]最小值 res[2]总和 res[3]平均值
                res[0] = nums[0];
                res[1] = nums[0];
                res[2] = 0;
                for (int i=0;i<nums.Length; i++) {
                    //如果循环的元素比最大的大就放进最大值
                    if (nums[i] > res[0])
                    {
                        res[0] = nums[i];
                    }
                    else if(nums[i]<res[1]){
                        //如果循环的元素比最小的小就放进最小值
                        res[1] = nums[i];
                    }
                    //求总和
                    res[2] += nums[i];
                }
                //平均值
                res[3] = res[2] / nums.Length;
                return res;
            }
            /// <summary>
            /// out返回一个数组中的最大值,最小值,总和、平均值
            /// </summary>
            /// <param name="num"></param>
            /// <param name="max"></param>
            /// <param name="min"></param>
            /// <param name="sum"></param>
            /// <param name="avg"></param>
            public static void Test(int[] nums, out int max, out int min, out int sum, out int avg)
            {
                //out要求在方法内部为其赋值
                max = nums[0];
                min = nums[0];
                sum = 0;
                for (int i = 0; i < nums.Length; i++)
                {
                    //如果循环的元素比最大的大就放进最大值
                    if (nums[i] > max)
                    {
                        max = nums[i];
                    }
                    else if (nums[i] < min)
                    {
                        //如果循环的元素比最小的小就放进最小值
                        min = nums[i];
                    }
                    //求总和
                    sum += nums[i];
                }
                avg = sum / nums.Length;
            }
        }
    }
    
    
    运行结果
    2、ref

    能将一个参数带进方法进行改变,将改变后的值带出方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace day_01_4
    {
        class Program
        {
            static void Main(string[] args)
            {
                double salary = 5000;
                jiangjin(ref salary);
                Console.WriteLine(salary);
                Console.ReadKey();
            }
            public static void jiangjin(ref double s) {
                s += 500;
            }
        }
    }
    
    运行结果
    3、params

    可变长度参数,将实参列表中跟可变参数元素一致的元素当做数组来处理

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace day_01_5
    {
        class Program
        {
            static void Main(string[] args)
            {
                //params可变参数数组
                Test(1, 2);
                Test(1, 2, 3, 4);
                Console.ReadKey();
            }
            public static void Test(params int[] score) {
                int sum = 0;
                for (int i = 0; i < score.Length;i++) {
                    sum += score[i];   
                }
                Console.WriteLine(sum);
            }
        }
    }
    
    运行结果
    4、方法的重载

    方法的重载就方法的名称相同参数不同

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace day_01_5
    {
        class Program
        {
            static void Main(string[] args)
            {
                m1(1);
                m1(1, 2);
                m1(1, 2, 3);
                Console.ReadKey();
            }
            //重载
            public static void m1(int n1) {
                Console.WriteLine(n1);
            }
            public static void m1(int n1,int n2)
            {
                Console.WriteLine(n1+n2);
            }
            public static void m1(int n1,int n2,int n3)
            {
                Console.WriteLine(n1+n2+n3);
            }
        }
    }
    
    
    运行结果
    5、方法的递归

    方法自己调用自己

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace day_01_5
    {
        class Program
        {
            static void Main(string[] args)
            {
                //方法的递归
                TellStoty();
                Console.ReadKey();
            }
            //讲10遍故事
            public static int i = 0;
            public static void TellStoty() {
                Console.WriteLine("从前有座山");
                Console.WriteLine("山里有座面");
                Console.WriteLine("庙里有个老和尚和一个小和尚");
                Console.WriteLine("老和尚给小和尚讲故事");
                i++;
                if (i >= 10) {
                    return;
                }
                TellStoty();
            }
        }
    }
    
    
    运行结果

    基础太简单不再赘叙

    相关文章

      网友评论

        本文标题:C#语法基础

        本文链接:https://www.haomeiwen.com/subject/fwxhqqtx.html