一道关于 C# linq where 的面试题

十年河东,十年河西,莫欺少年穷

学无止境,精益求精

            List<int> artys = new List<int>() { 1,2,3,4,5,6,7,8};
            //
            var s = artys.Where(A => A > 5);

面试官让你写一个方法来实现Where,怎么写?

本篇提供两种方法,如下:

复制代码
    internal class Program
    {
        //委托是指向方法的类型 
        static void Main(string[] args)
        {
            List<int> artys = new List<int>() { 1,2,3,4,5,6,7,8};
            //
            var s = artys.Where(A => A > 5);
            var Rs1 = MyWhere(artys, A => A > 5);
            foreach(var item in Rs1)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("-----------------------");
            List<string> strArtys = new List<string>() { "jack","tom","luc"};
            var Rs2 = MyWhereYield(strArtys, A => A == "tom");
            foreach (var item in Rs2)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }

       
        static IEnumerable<T> MyWhere<T>(IEnumerable<T> items ,Func<T,bool> f)
        {
            List<T>  results = new List<T>();
            foreach(var item in items)
            {
                if (f(item))
                {
                    results.Add(item);
                } 
            }
            return results;
        }

        static IEnumerable<T> MyWhereYield<T>(IEnumerable<T> items, Func<T, bool> f)
        { 
            foreach (var item in items)
            {
                if (f(item))
                {
                    yield return item;
                }
            } 
        }
    }
复制代码

本篇示例用到了委托,拉姆达表达式等

一个简单的示例:

复制代码
using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //委托
            f1 f_1 = add1;
            f1 ff1 = new f1(add1);//
            f2 f_2 = add2;
            f2 ff2 = new f2(add2);
            f3 f_3 = add3;
            f3 ff3 = new f3(add3);
            f4 f_4 = add4;
            f4 ff4 = new f4(add4);//
            f_1();
            f_2(1, 2);
            f_3();
            f_4(1,2);
            //
            Action A_1 = add1;
            Action<int ,int> A_2 = add2;
            Func<int> A_3 = add3;
            Func<int,int,int> A_4 = add4;
            A_1();
            A_2(1,2);
            A_3();
            A_4(1, 2);
            // 
            f1 f1 = delegate ()
            {
                Console.WriteLine("我和add1一样,我是匿名方法");
            };
            f1();

            f1 fff1 = () => { Console.WriteLine("我和add1一样,我是拉姆达表达式"); };
        }

       static void add1()
        {
            Console.WriteLine("我是add1");
        }
        static void add2(int a,int b)
        {
            Console.WriteLine("我是add2");
        }
        static int add3()
        {
            Console.WriteLine("我是add3");
            return 0;
        }

        static int add4(int a,int b)
        {
            Console.WriteLine("我是add4");
            return a + b;
        }

        delegate void f1();
        delegate void f2(int a, int b);
        delegate int f3();
        delegate int f4(int a, int b);
    }
}
View Code
复制代码

详情请参考:C# Lambda表达式    由浅到深讲解C#-LINQ    C# 匿名方法

@天才卧龙的博客

posted @   天才卧龙  阅读(295)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示