【第十天】10c#今日小结

1.Lambda表达式

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

namespace _02Lambda表达式
{
    internal class Program
    {
        static void Main(string[] args)
        {

            //lambda表达式:可以让我们定义一个变量接收一个函数,主要是作为另外一个
            //函数的参数进行使用,具体体现变量和函数体之间使用=>,所以、
            //其他编程语言称之为箭头函数,但是严谨来说c#没有箭头函数。

            //如果一个函数仅在当前作用域进行使用 可以把函数定义在当前作用域就行
            int Sum(int a ,int b)
            {
                return a + b;
            }
            Console.WriteLine(Sum(10,20));//30

            //定义lambda表达式
            //方法的组成: 参数列表、返回值 、方法名 方法体

            //1使用Func关键字接收有返回值的lambda表达式
            //Func<> 方法类型、函数类型
            //第一个类型 是参数1的类型
            //第二个类型 是参数2的类型
            //第三个类型 是返回值的类型
            //f1 变量名
            //(a, b) 参数类别 不用写类型,多个参数时候加()
            // => 表达式
            //{}方法体
            // 有返回值的添加return 关键字。
            Func<int,int ,int>  f1 = (a, b) => { return a + b; };
            Console.WriteLine(f1(10,20));// 可以通过变量名调用函数


            //  2使用Action关键字接收无返回值的lambda表达式
            //定义一个字符串参数 无返回值的lambda表达式
            // 如果一个参数可以把()不写
            // 如果函数体只有一句可以把{}省掉
            Action<string>  f2 = (a) => Console.WriteLine(a);
            f2("hell world");


            //3 使用Predicate<>关键字接收返回值为bool类型的lambda表达式
            Predicate<int> f3 = a=> a %2==0;
            Console.WriteLine(f3(3));//false

            Func<int,bool> f4 = a=> a%3==0;
            Console.WriteLine(f4(9));//true


            /*Func<参数类型,参数类型,返回值类型> f1 = (a,b)=>{return a+b}  
             * 可以接受有返回值的lambda表达式 参数个数没有限制、返回值的类型也是没有限制
             * 
             * Action<string> f2 = a =>Console.WriteLine(a);
             *  只能接收无返回值的lambda表达式
             * 
             * Predicate<int> f3 = a=> a %2==0;
             * 只能接收返回值为bool类型的lambda表达式
             */

            // lambda具体的使用场景:最为另外一个函数参数进行使用,数组的查询方法当中使用
            int[] ints = { 1, 2, 3, 4,6,9 };
            Console.WriteLine(Array.Find(ints, a => a % 2 == 0));//2 

            int[]  ss =  Array.FindAll(ints,v=>v%3==0);//找到所有的数组
            // 第一种遍历数组
            for (int i = 0; i < ss.Length; i++)
            {
                Console.WriteLine(ss[i]+"-------");
            }

            //第二种遍历数组
            foreach (var item in ss)
            {
                Console.WriteLine(item+"+++++++");
            }

            // 第三种遍历数组
            Array.ForEach(ss, v => Console.WriteLine(v + "?????"));


            //where  找到所有满足条件的元素
            foreach (var item in ints.Where(v => { return v % 3 == 0; }))
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
       
        
    }
}

2.可空类型数据

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

namespace _03可空类型数据
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //空类型是null,引用类型的变量都可以赋值成null
            string s = null;
            int[] ints = null;
            object  o = null;

            // 基本数据类型不能赋值为null的
            //int a = null; 报错

            //如果想让基本数据类型能够赋值为null 可以使用?
            int? a = null; // a以后可以等于null 在原先的范围内加一个null赋值范围
            a = 100;// a可以再赋值100
           // int sum = a + 100; 报错 a是可空类型,但是sum不是可空类型


          //即想使用可空类型 又想使用基本类型进行运算,可以把可空类型再加一个问号 使用双??
            int ? b = null; // b可空类型
            b = 300;
            int aa = b ?? 100;  // b?? 如果b是null aa的值100, 如果b不是null aa的值300
            Console.WriteLine(aa); //300


            int? c = null;
            int d = c ?? 100;
            Console.WriteLine(d);//100


            c = 999;
            int sum = (c ?? 100) + 200;
            Console.WriteLine(sum);// 1199


           


            Console.ReadLine();
        }
    }
}

3.动态数组ArrayList

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

namespace _04动态数组ArrayList
{
    class People
    {
        public string Name { get; set; }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            // ArrayList:动态数组一个不固定长度的数组集合,并且可以存储不同
            // 类型的元素,例如一个arrayList对象可以存储整型、字符串、对象、数组等,
            //但是int[]这种数组一旦创建好之后 不能后续添加元素,也就是长度
            //是固定,并且一个数组对象只能存储一种类型的元素

            //ArrayList 弊端:取出元素是一个object 必须对其在进行类型转换
            

            //1 创建一个arrayList对象
            int[] ints = new int[] { 1, 2, 3 };
            People p1 = new People() { Name="zs"};
            ArrayList arr = new ArrayList() { 1,2,3,"坤拳",true,ints,p1}; //初始化添加元素

            //2 取出元素 通过索引值取元素 arr[1]
            //int aa = arr[0]; //数组取出的元素是object类型,如果想转成其他类型 必须强制转换
            Console.WriteLine( Convert.ToInt16(arr[1]));

            //3 清空元素
            arr.Clear();
            
            //4 添加元素
            arr.Add(1); 
            arr.Add("说得好");//添加一个元素
            arr.AddRange(ints);//也可以添加一个数组

            //5删除元素
            arr.Remove("说得好"); //删除指定元素
            arr.RemoveAt(0);// 删除指定位置元素
            arr.RemoveRange(0,3);// 从指定参数1位置开始删除参数个数的元素

            //6 数组插入
            arr.Add(12);
            arr.Add(24);
            arr.Insert(1, 36);//在参数1这个位置插入参数2这个元素
            //arr.InsertRange(0,ints); //在指定位置插入一个数组

            //7修改元素
            arr[1] = 48;

            //8数组排序
            arr.Sort(); //默认的是升序排序

            //9indexOf() 元素的索引值
            Console.WriteLine(arr.IndexOf(48));

            //10count 获取数组的个数
            Console.WriteLine(arr.Count);

            //11 Reverse();把数组里面的元素进行倒序
             arr.Reverse();

            //12toArray(); 把ArrayList转成Array(int[])
            object[] ii= arr.ToArray();   

            //ArrayList 类; arr 对象
            

            //8查询可以查询一个arr[1],查询多个for 遍历
            //最后算法遍历元素
            //如果数组元素类型不一样遍历的时候把类型改成object
            foreach (object i in arr) 
            {
                Console.WriteLine(i+"---------");
            }



            
            ArrayList list = new ArrayList() { 1,2,3,3,3,2,7};

            Console.ReadKey();
        }
    }
}

5.List

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

namespace _05List
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Array数组 int[]

            //ArrayList 动态数组 存储元素类型object类型,取出的时候转成对应的类型,类型不安全

            // List:方法和ArrayList很像 区别在于list可以指定类型进行存储,存储同
            // 一种类型数据 ,取出时候不用强制转换,保证数据类型安全
            List<int> list1 = new List<int>(); //创建集合对象

            //1 添加元素
            list1.Add(1);
            list1.Add(2);
            list1.Add(3);
            list1.AddRange(new int[] {9,10,11}); //添加多个

            //2 删除
            list1.Remove(11);//删除指定元素
            list1.RemoveAt(list1.Count-1);//删除指定位置的元素
            list1.RemoveAll(v => v % 3 == 0);//删除满足条件所有的元素

            //3修改
            list1[0] = 20;


            //4插入数据
            list1.Insert(1, 10);//在索引值为1的地方插入888

            //5IndexOf() 获取元素的索引值
            Console.WriteLine(list1.IndexOf(888));

            //6 Clear()清空集合
            //list1.Clear(); 清空集合

            //7Any() 有一个满足条件就为true
            Console.WriteLine(list1.Any(v => v > 888));

            //8Average() 求序列中元素的平均值
            Console.WriteLine(list1.Average());

            //9Concat() 拼接俩个数组,返回的是一个新数组
             list1.Concat(new int[] {36});

            //10Contains() 数组是否包含2
            Console.WriteLine(list1.Contains(2));

            //11Sum() 求和
            Console.WriteLine(list1.Sum());

            //12Skip()  跳过自定的个数,返回剩余的 
            int[] ss = list1.Skip(1).ToArray();

            //13Max() 找出序列元素最大值
            Console.WriteLine(list1.Max());



            //查询多个使用遍历,查询一个使用索引值arr[0]
            foreach (int i in list1)
            {
                Console.WriteLine(i+"-------");
            }

           
            int[] ss2 = new int[] { 1, 2, 4 }; //数组
            //list1 定义集合{20,10,2} 针对list1集合里面的元素进行操作,不做任何计算
            //ss2 { 1, 2, 4 } 针对list2里面元素进行*10的操作  变成了{10,20,40}
            //(x,y)=>new People(){ Num1=x,Num2=y }   对变化之后的xy添加到People的Num1和Num2属性上
            //方法的结果一个集合,list1的20 与ss的2*10对应上, 把对应关返回了
            //也就是20 ----- 2作为匹配结果
            // 10 ----1 也是匹配上
             var result = list1.Join(ss2,
                x=>x,
                y=>y*10,
                (x,y)=>new People(){ Num1=x,Num2=y }               
                );
        
            foreach (var item in result)
            {
                Console.WriteLine(item.Num1+"-----"+ item.Num2);
            }
          
            Console.ReadKey();
        }
    }
    class People
    {
        public int Num1 { get; set; }
        public int Num2  { get; set; }
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值