c#-初级篇合集

001-学习c#编程



//这是一条注释,下面是引入命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//定义命名空间,从“{”到“}”结束
namespace _001_学习c_sharp编程
{
    //定义一个类
    class Program
    {
        //定义一个Main方法,程序的入口,有且仅有一个
        static void Main(string[] args)
        {
            //方法体
            Console.Write("Hello World");//在输出末尾不加换行符
            Console.WriteLine("Hello World");//在输出末尾加换行符
            Console.WriteLine("Hello World !{0}+{1}={1}",0,4);//在输出末尾加换行符
        }
    }
}

002-变量

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _002_变量
{
    class Program
    {
        static void Main(string[] args)
        {
            int age = 20;//声明变量
            int hp = 60;
            string name = "pr";
            Console.WriteLine(name);

            Console.ReadKey();
        }
    }
}

003-变量的类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _003_变量的类型
{
    class Program
    {
        static void Main(string[] args)
        {
            byte myByte = 34;
            int score = 6000;
            long count = 10000000030000;
            Console.WriteLine("byte:{0}int:{1}long:{2}",myByte,score,count);
            float myFloat = 12.5f;//小数默认是double类型,加f声明为float类型。


            Console.WriteLine("byte:{0}int:{1}long:{2}float{3}", myByte, score, count,myFloat);

            char myChar = 'a';//字符
            string myString1 = "a";//字符串
            string myString2 = "";//空字符串
            bool myBool = false;//布尔型变量,true或者false,只能小写,大写(如:False)不能识别,会报错

            Console.WriteLine("char:{0} string:{1} string:{2} bool:{3}",myChar,myString1,myString2,myBool);

            Console.ReadKey();
        }
    }
}

004-练习 定义变量储存主角信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _004_练习_定义变量储存主角的信息
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "李丹兰";
            int hp = 100;
            int level = 34;
            float exp = 345.3f;
            Console.WriteLine("主角的名字是:\n\"{0}\"\n 血量是:\"{1}\"\n 等级是:\"{2}\"\n 经验是:\"{3}\"",name,hp,level,exp);



            Console.ReadKey();
        }
    }
}

005-在字符串前面加上字符@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _005_在字符串前面加上字符
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "I'm a good man,\n but you are a bad girl.";
            string str2 = @"I'm a good man. \n  you are a bad girl. ";  
            Console.WriteLine(str1);
            Console.WriteLine(str2);
            //@的左右
            //1.加@使不识别字符串中的转义字符,除了双引号(使用两个引号表示一个引号)
            //2.允许字符串定义在多行
            //string str3 = "I'm a good man,
            //    \n but you are a bad girl.";
           string str3 = @"I'm a good man,
                \n but you are a bad girl.";
           Console.WriteLine(str3);

            //3.在表示路径时
           string path1 = "c:\\csharp\\study";
           string path2 = @"c:\csharp\study";
           Console.WriteLine(path1);
           Console.WriteLine(path2);//path1 与 path2 输入结果是一致的。

            Console.ReadKey();
            
        }
    }
}

006-变量的声明和赋值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _006_变量的声明和赋值
{
    class Program
    {
        static void Main(string[] args)
        {
            //Ctrl+K 然后 Ctrl+c 这是个组合快捷键 可以注释选中行
            //Ctrl+K 然后 Ctrl+u 取消注释选中行
            int age;
            age = 25;
            int age2 = 25;//第一次给变量赋值叫初始化,否则叫赋值,变量没被初始化就无法使用。
            int hp, mp = 90, exp = 90;//一行中可给多个变量初始化
         

        }
    }
}

007-数学运算符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _007_数学运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            int number1 = 45;
            int number2 = 90;
            int result = 0;
            result = number1 + number2;
            int result2;
            result2 = number1 * number2;
            int result3;
            result3 = number1 - number2;
            int result4;
            result4 = number1 / number2;//取整数
            //当两边操作数类型一样时,返回值类型一样
            //当两边操作数类型不一样时,返回值类型是大类型。
            int num3 = 45;
            double d = 34.5;
           // int num4 = num3 + d;出错
            double num4 = num3 + d;
            Console.WriteLine("{0}\n{1}\n{2}\n{3}",result,result2,result3,result4);
            //关于加法运算符更多的使用
            //1.字符串相加
            string str1 = "你的名字是";
            string str2 = "李丹兰";
            string str3 = str1 + str2;
            Console.WriteLine(str3);//"你的名字是李丹兰"

            //2.当一个字符串跟一个数字相加的话,首先把数字转变成字符串,然后连接起来,结果是字符串
            string str4 = "1234";
            num4 = 5667;
            str4 = str4 + num4;
            Console.WriteLine(str4);
            Console.ReadKey();

        }
    }
}

008-数学运算符自加自减

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _008_数学运算符_自加自减
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 45;
            num1++;
            Console.WriteLine(num1);
            ++num1;//++不管放在操作数的前面还是后面,都是让操作数加1.++在前先自加后取值,++在后先取值后自加。
            Console.WriteLine(num1);
            --num1;
            num1--;//同理
            Console.WriteLine(num1);
            Console.ReadKey();

        }
    }
}

009-从键盘上读取输入的字符串

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009_从键盘上读取输入的字符串
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = Console.ReadLine();//输入字符串
            Console.WriteLine(str1);

            string str2 = "123";
            int num1 = Convert.ToInt32(str2);
            num1++;
            Console.WriteLine(num1);

            string str3 = Console.ReadLine();
            double num2 = Convert.ToDouble(str3);

            Console.WriteLine(num2+11);
            
            Console.ReadKey();
        }
    }
}

010-接收输入显示和

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _010_接收输入_显示和
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = Console.ReadLine();
            int num1 = Convert.ToInt32(str1);
            string str2 = Console.ReadLine();
            int num2 = Convert.ToInt32(str2);
            int num3 = num1 + num2;
            Console.WriteLine("{0}+{1}={2}\n",num1,num2,num3);

            Console.ReadKey();



        }
    }
}

011-赋值运算符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _011赋值运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 34;//最基本的赋值运算符
            num1 += 12;//num1 = num1+12;
            num1 *= 12;//num1 = num1*12;
            num1 -= 12;//num1 = num1-12;
            num1 /= 12;//num1 = num1/12;
            num1 %= 12;//num1 = num1%12;
        }
    }
}

012-运算符的优先级

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _012_运算符的优先级
{
    class Program
    {
        static void Main(string[] args)
        {

            int num = 12 + 90;
            int num1 = 12 + 90 * 2 / 4 % 4;//在程序中,运算是有优先级的,先进行优先级高的,然后低的
            int num2 = (12 + 90) * 2;//括号可以改变优先级。
            Console.WriteLine(num);
            Console.WriteLine(num1);
            Console.WriteLine(num2);



            Console.ReadKey();//暂停控制台

        }
    }
}

013-练习一和二

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _013_练习一和二
{
    class Program
    {
        static void Main(string[] args)
        {

            //int hp;//血量
            //float x,y,z;//坐标
            //float speed ;//速度
            string str1 = Console.ReadLine();
            int num1 = Convert.ToInt32(str1);
            string str2 = Console.ReadLine();
            int num2 = Convert.ToInt32(str2);
            num1 = num1 + num2;
            num2 = num1 - num2;
            num1 = num1 - num2;
            Console.WriteLine("num1 = {0}\nnum2 = {1}", num1, num2);
            Console.ReadKey();
          


        }
    }
}

014-练习三和四

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _014_练习三和四
{
    class Program
    {
        static void Main(string[] args)
        {
            //练习3:输入4个数字,输出他们的乘积
            string str1, str2, str3, str4;
            str1 = Console.ReadLine();
            str2 = Console.ReadLine();
            str3 = Console.ReadLine();
            str4 = Console.ReadLine();
            int num1, num2, num3, num4;
            num1 = Convert.ToInt32(str1);
            num2 = Convert.ToInt32(str2);
            num4 = Convert.ToInt32(str4);
            num3 = Convert.ToInt32(str3);
            Console.WriteLine(num1 * num2 * num3 * num4);
            //练习4:输入一个整数,输出它的逆序数字,例如:输入234,输出432
            string str = Console.ReadLine();
            int num = Convert.ToInt32(str);
            int bai = num / 100;
            int shi = num / 10;
            int ge = num % 10;
            Console.WriteLine(ge+""+shi+""+bai);

           
            Console.ReadKey();

        }
    }
}

015-练习五和六

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _015_练习56
{
    class Program
    {
        static void Main(string[] args)
        {
           //5.输入梯形的上底和下底、高,输出梯形面积

            string str1, str2, str3;
            Console.WriteLine("请输入梯形的上底:");
            str1 = Console.ReadLine();
            Console.WriteLine("请输入梯形的下底:");
            str2 = Console.ReadLine();
            Console.WriteLine("请输入梯形的高:");
            str3 = Console.ReadLine();
            double sd, xd, h;
            sd = Convert.ToDouble(str1);
            xd = Convert.ToDouble(str2);
            h = Convert.ToDouble(str3);
            double area = (sd + xd) * h / 2.0;
            Console.WriteLine("梯形的面积为:{0}",area);
            

            //6.输入圆的半径,计算圆的面积
            Console.WriteLine("请输入圆的半径:");
            string str_r = Console.ReadLine();
            double d_r = Convert.ToDouble(str_r);
            double areaOfCircle = d_r * d_r * 3.14;
            Console.WriteLine("圆的面积为:{0}", areaOfCircle);




       
            Console.ReadKey();
        }
    }
}

016-布尔运算

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _016_布尔运算
{
    class Program
    {
        static void Main(string[] args)
        {

            bool res = 12<4;
            Console.WriteLine(res);
            Console.WriteLine(!res);
            Console.ReadKey(res);
        }
    }
}

017-布尔运算符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _017_布尔运算符
{
    class Program
    {
        static void Main(string[] args)
        {

            bool res = 12 <= 5;
            bool res1 = 12 >= 5;
            bool res2 = 12 < 5;
            bool res3 = 12 > 5;
            bool res4 = 12 == 5;
            bool res5= 12 != 5;
            Console.WriteLine(res);
            Console.WriteLine(res1);
            Console.WriteLine(res2);
            Console.WriteLine(res3);
            Console.WriteLine(res4);
            Console.WriteLine(res5);

            Console.ReadKey();
        }
    }
}

018-条件布尔运算符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _018_条件布尔运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            bool val1 = true;
            bool val2 = false;
            bool res1 = val1 & val2;//且运算,false
            bool res2 = val1 | val2;//或运算,true;
            bool res3 = !val1;//取反运算,false;
            bool res4 = val1 ^ val2;//异或运算,true
            bool res5 = val1 && val2;//且运算,false
            Console.WriteLine(res1);
            Console.WriteLine(res2);
            Console.WriteLine(res3);
            Console.WriteLine(res4);
            Console.WriteLine(res5);



            Console.ReadKey();
        }
    }
}

019-关于goto语句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _019_关于goto语句
{
    class Program
    {
        static void Main(string[] args)
        {

        //使用格式:
            //goto<标签>;
            //<标签>:

            int num = 8;
            goto mylabel;
            num++;
            mylabel:
            Console.WriteLine(num);

            Console.ReadKey();
        }
    }
}

020-if语句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _020_if语句
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入一个数字:");
            string str = Console.ReadLine();
            int num = Convert.ToInt32(str);
            if (num > 50)
                Console.WriteLine("您输入的数字大于50");
            if (num < 50)
                Console.WriteLine("您输入的数字小于50");

            Console.ReadKey();
        }
    }
}

021-三元运算符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _021_三元运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 10;
            string str = (num > 100) ? "该数大于100" : "该数小于10";
            Console.WriteLine(str);
            Console.ReadKey();
        }
    }
}

022-使用if-else-if-else语句做条 件判断

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _022_使用if_else_if_else语句做多条件判断
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入您的分数:");
            string str = Console.ReadLine();
            int score = Convert.ToInt32(str);
            if (score >= 90)
            {
                Console.WriteLine("优秀");
            }
            else if (score >= 80)
            {
                Console.WriteLine("良好");
            }
            else if (score >= 70)
            {
                Console.WriteLine("一般");
            }
            else if (score >= 60)
            {
                Console.WriteLine("及格");

            }
            else
            {
                Console.WriteLine("不及格");
            }

            Console.ReadKey();
        }
    }
}

023-switch语句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _023_switch语句
{
    class Program
    {
        static void Main(string[] args)
        {

            int state = 3;
            switch(state)
            {
                case 1 
            }
            Console.ReadKey();
        }
    }
}

024-while循环

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _024_while循环
{
    class Program
    {
        static void Main(string[] args)
        {
            //while (true)
            //{
            //    Console.WriteLine("www.baidu.com");
            //}
            int index = 1;
            while (index < 9)
            {

                Console.WriteLine(index);
                index++;
            }

        }
    }
}

025-do循环

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _025_do循环
{
    class Program
    {
        static void Main(string[] args)
        {

            int index = 1;
            do
            {
                Console.WriteLine(index++);
            }while(index<9);
        
        Console.ReadKey();
        }
    }
}

026-for循环

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _026_for循环
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();
        }
    }
}

027-循环的终止break

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _027_循环的终止break
{
    class Program
    {
        static void Main(string[] args)
        {

            int index = 1;
            while (true)
            {
                if (index == 10) break;

                Console.WriteLine(index++);
            
            }
            Console.ReadKey();
        }
    }
}

028-循环的中断continue

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _028_循环的中断continue
{
    class Program
    {
        static void Main(string[] args)
        {

            int index = 0;
            while (true)
            {
                index++;
                if (index == 5)
                    continue;
                if (index == 10)
                    break;
                Console.WriteLine(index);
            }
            Console.ReadKey();
        }
    }
}

029-goto和return跳出循环

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _029_goto和return跳出循环
{
    class Program
    {
        static void Main(string[] args)
        {
            int index = 0;
            while (true)
            {
                if (index == 7)
                goto mylabel;
                Console.WriteLine(index++);
            }
        mylabel:
            return;
            Console.ReadKey();
        }
    }
}

030-显示转换和隐式转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _030_显示转换和隐式转换
{
    class Program
    {
        static void Main(string[] args)
        {
            byte myByte = 123;
            int myInt = myByte;//把一个小类型的数据 复制给大类型的变量的时候,编译器会自动进行类型的转换
            myByte = (byte)myInt;//当把一个大类型赋值给一个小类型的变量的时候,需要进行显示转换(也称强制类型转换),就是加上括号和类型

            string str = "123";
            int num = Convert.ToInt32(str);

            int mynum = 232324;
            string str2 = Convert.ToString(mynum);
            str2 = mynum + "";
            Console.WriteLine(num);
            Console.ReadKey();
        }
    }
}

031-枚举类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _031_枚举类型
{
    //枚举类型的定义 去声明变量
    enum GameState   //:byte,将类型变成byte
    {
        Pause=11,//默认代表整数0
        Failed,//默认代表整数1
        Success,//默认代表整数2
        Start//默认代表整数3
    }
    class Program
    {
        static void Main(string[] args)
        {
            GameState state = GameState.Start;
            if (state == GameState.Start)
            {
                Console.WriteLine("当前游戏处于开始状态");
            }
            int num = (int)state;
            Console.WriteLine(num);
            Console.WriteLine(state);
            Console.ReadKey();

        }
    }
}

032-结构体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _32_结构体
{//将几个类型组合成新的类型
    struct Position
    {
        public float x;
        public float y;
        public float z;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Position ep1;
            ep1.x = 100;
            ep1.y = 11;
            ep1.z = 11;
        }
    }
}

033-数组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _033_数组
{
    class Program
    {
        static void Main(string[] args)
        {
            //第一种初始化方式
            int[] scores={23,34,5,5,4,3,4};//使用这种方法赋值时,只能在声明数组。
            //第二种数组创建的方式
            int[] scoress = new int[10];
            int[] scores1;
            scores1 = new int[10];
            int[] scores2 = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            Console.WriteLine(scores2[2]);//当我们访问一个引索不存在的值时,会出现异常。


            string[] names = new string[] { "taiker", "baidu", "goole" };


            Console.ReadKey();
        }
    }
}

034-数组的遍历

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _034_数组的遍历
{
    class Program
    {
        static void Main(string[] args)
        {
            //第一种遍历方式
            int[] scores = { 1, 2, 3, 4, 4, 5, 5, 5, 5 };
            for (int i = 0; i < scores.Length; i++)
            {
                Console.WriteLine(scores[i]);
            }


            //第二种遍历方式
            int id= 0;
            while(id<scores.Length)
            {
                Console.WriteLine(scores[id++]);
            }
            第三种遍历方式
            foreach (int temp in scores)
            {
                Console.WriteLine(temp);
            
            }
            Console.ReadKey();
        }
    }
}

035-字符串的处理

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _035_字符串的处理
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = " www.taikr.Com  ";
            for (int i = 0; i < str.Length; i++)
            {
                Console.WriteLine(str[i]);
            }
            string res = str.ToLower();//变小写,返回副本,对原字符串没有影响
            string res1 = str.Trim();//移除首尾空格字符,对原字符串没有影响
            string res2 = str.TrimEnd();
            string res3 = str.TrimStart();
           Console.WriteLine(res);
           Console.WriteLine(res1+"|");
           Console.WriteLine(res2+"|");
           Console.WriteLine(res3+"|");
          
           string[] strArray = str.Split('.');
            foreach(string temp in strArray)
            {
                Console.WriteLine(temp);
            }
            Console.ReadKey();
        }
    }
}

036-函数的定义

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _036_函数的定义
{
    class Program
    {
        static void Write()
        {
            //这里是函数体也是方法体,这里可以写0行,也可多行
            Console.WriteLine("Text output from function");
        
        }
        static int add(int a, int b)
        {
            return a + b;
        }
        static void Main(string[] args)
        {
            Write();
            Console.WriteLine(add(23,323));
            Console.ReadKey();
        }
    }
}

037-函数的定义和使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _037_函数的定义和使用
{
    class Program
    {
        static int[] GetDivisor(int num)
        {
            int count = 0;
            for (int i = 1; i <= num; i++)
            {
                if (num % i == 0)
                {
                    count++;
                }
            }
            int[] array = new int[count];
            int index = 0;
            for (int j = 1; j <= num; j++)
            {
                if (num % j == 0)
                    array[index++] = j;
            
            }
            return array;

        }
        static void Main(string[] args)
        {

            int[] array = GetDivisor(9);
            foreach (int temp in array)
            {
                Console.WriteLine(temp);
            
            }
            Console.ReadKey();

        }
    }
}

038-参数数组-定义一个参数个数不确定的函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _038_参数数组_定义一个参数个数不确定的函数
{
    class Program
    {
        static int Plus(params int[] array)//这里定义了一个int类型参数数组
        {
            int res = 0;
            int n = array.Length;
            for (int i = 0; i < n; i++)
            {
                res += array[i];
            }
            return res;
        
        }
        static void Main(string[] args)
        {

            int res = Plus(1,3,3,4,4,4,4,23);//参数数组就是帮我们减少了一个创建数组的过程
            Console.WriteLine(res);
            Console.ReadKey();
        }
    }
}

039-结构函数的定义和使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _039_结构函数的定义和使用
{

    struct CustomerName
    {
        public string firstName;
        public string lastName;
        public string GetName()
        {
            return firstName + " " + lastName;
        }
    }
    struct Vector3
    {
        public float x;
        public float y;
        public float z;
        public float Distance()
        {
            return (float)Math.Sqrt(x * x + y * y + z * z);
        }

    
    }
    class Program
    {
        static void Main(string[] args)
        {
            CustomerName myName;
            myName.firstName = "PENG";
            myName.lastName = "RUI";
            Console.WriteLine("My name is "+myName.GetName());
            Console.ReadKey();
        }
    }
}

040-函数的重载

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _040_函数的重载
{
    class Program
    {
        //函数的重载是指函数名相同时,通过参数类型、数量不同,来实现不同的功能
        static int MaxValue(params int[] array)
        {
            int maxValue = array[0];
            for (int i = 1; i < array.Length; i++)
            {
                if (array[i] > maxValue)
                {
                    maxValue = array[i];
                
                }
            }
            return maxValue;
       }
        static double MaxValue(params double[] array)
        {
            double maxValue = array[0];
            for (int i = 1; i < array.Length; i++)
            {
                if (array[i] > maxValue)
                {
                    maxValue = array[i];

                }
            }
            return maxValue;
        }
        static void Main(string[] args)
        {
            int res = MaxValue(232,4,4,5,100,3,3);//编译器会根据你传递过来的实参的类型去判定调用哪一个函数
            double res1 = MaxValue(2.4, 22, 3443.9, 22);

            Console.WriteLine(res);
            Console.WriteLine(res1);
            Console.ReadKey();

        }
    }
}

041-委托的使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _041_委托的使用
{
    //定义一个委托跟函数差不多,区别在于
    //1,定义委托需要加上关键字delegate
    //2,委托不需要函数体
    public delegate double myDelegate(double param1,double param2);
    class Program
    {
        static double Multiply(double param1, double param2)
        {
            return param1 * param2;
        }
        static double Divide(double param1, double param2)
        {
            return param1 / param2;
        }
        static void Main(string[] args)
        {
            myDelegate de;//利用我们定义的委托类型声明一个新的变量
            de = Multiply;//当我们给一个委托的变量赋值的时候,返回值类型跟参数表必须一样,否则无法赋值
            Console.WriteLine(de(2.0,34.1));
            de = Divide;
            Console.WriteLine(de(2.0,34.1));
            Console.ReadKey();

        }
    }
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值