博客
关于我
LINQ之ElementAt,ElementAtOrDefault
阅读量:318 次
发布时间:2019-03-04

本文共 2379 字,大约阅读时间需要 7 分钟。

目录

ElementAt()

对于数组和List类,[]提供了索引器,因此很容易在指定的索引处获取元素。除了用[]访问以外,我们还可以用ElementAt()获取元素。并且ElementAt()可以获取所有继承IEnumerable<T>类的元素。

public static TSource ElementAt<TSource>( this IEnumerable<TSource> source, int index );

代码示例:

public static class Program{   	//指定索引    private static readonly int SELECTED_INDEX = 4;    static void Main( string[] args )    {           int[] numbers = new int[] {    1, 2, 3, 5, 7, 11 };        int result  = numbers.ElementAt( SELECTED_INDEX );        System.Console.WriteLine( "数据:{0}", numbers.Text() );        System.Console.WriteLine( "指定索引:{0}", SELECTED_INDEX );        System.Console.WriteLine( "结果:{0}", result );        System.Console.ReadKey();    }    public static string Text
( this IEnumerable
i_source ) { string text = string.Empty; foreach( var value in i_source ) { text += string.Format( "[{0}], ", value ); } return text; }} 数据:[1], [2], [3], [5], [7], [11],指定索引:4結果:7

因为ElementAt()作用和[]差不多,所以使用的比较少。不过用在自己创建的序列类上还是比较好用的。

ElementAtOrDefault()

无论是ElementAt()还是[]都会存在一个问题,就是在超出索引范围的情况下会报异常,而ElementAtOrDefault()就不会有这个问题。在出现异常的情况下会返回该类型的默认值

public static TSource ElementAtOrDefault<TSource>( this IEnumerable<TSource> source, int index );

代码示例:

public static class Program{       //负数    private static readonly int SELECTED_INDEX = -1;    static void Main( string[] args )    {           int[] numbers = new int[] {    1, 2, 3, 5, 7, 11 };                int result = 0;        try        {               result  = numbers.ElementAtOrDefault( SELECTED_INDEX );        }        catch( System.Exception i_exception )        {               System.Console.WriteLine( "异常:{0}", i_exception );            System.Console.ReadKey();            return;        }        System.Console.WriteLine( "数据:{0}", numbers.Text() );        System.Console.WriteLine( "指定索引:{0}", SELECTED_INDEX );        System.Console.WriteLine( "结果:{0}", result );        System.Console.ReadKey();    }    public static string Text
( this IEnumerable
i_source ) { string text = string.Empty; foreach( var value in i_source ) { text += string.Format( "[{0}], ", value ); } return text; }} 数据:[1], [2], [3], [5], [7], [11],指定索引:-1结果:0

无论是超出索引范围或是使用负数,都会返回默认值。

但是这种便利也是一把双刃剑,比如说使用的int类型,你无法判断是找到了0还是返回的默认值

转载地址:http://bfnq.baihongyu.com/

你可能感兴趣的文章
nginx实现负载均衡
查看>>
Nginx实现限流
查看>>
Nginx将https重定向为http进行访问的配置(附Demo)
查看>>
nginx工作笔记004---配置https_ssl证书_视频服务器接口等
查看>>
nginx工作笔记005---nginx配置负载均衡_在微服务中实现网关集群_实现TCP传输层协议__http协议的负载均衡
查看>>
nginx常用命令及简单配置
查看>>
Nginx常用屏蔽规则,让网站更安全
查看>>
nginx开机启动脚本
查看>>
nginx异常:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf
查看>>
nginx总结及使用Docker创建nginx教程
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in usrlocalnginxconfnginx.conf128
查看>>
nginx日志分割并定期删除
查看>>
Nginx日志分析系统---ElasticStack(ELK)工作笔记001
查看>>
Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据
查看>>
Nginx映射本地静态资源时,浏览器提示跨域问题解决
查看>>
nginx最最最详细教程来了
查看>>
Nginx服务器---正向代理
查看>>
Nginx服务器上安装SSL证书
查看>>
Nginx服务器基本配置
查看>>