C# First and FirstOrDefault 方法詳解

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   sp   on   div   問題   

在工作中我們經常會遇到有關LINQ 的一些問題。這時我們就用到lambda 運算式。

下面是我在工作遇到的。 First  and FirstOrDefault  這兩方法。我今天把它記錄一下。

需要注意的是我標註紅色的部分,這是它們倆的區別。

First  and FirstOrDefault 
 1 #region
Enumberable First() or FirstOrDefault()
 2         /// <summary> 3         /// 返回序列中的第一個元素;如果序列中不包含任何元素,則返回預設值。  4         /// 如果 source 為空白,則返回 default(TSource);否則返回 source 中的第一個元素。 5         /// ArgumentNullException      sourcevalue 為 null。 6         /// </summary> 7         public static void FunFirstOrDefault() 8         { 9             //FirstOrDefault()10             string[] names = { "Haiming QI", "Har", "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu", null };11             // string[] names = { }; // string 類型的預設值是空12 13             int[] sexs = { 100, 229, 44, 3, 2, 1 };14             // int[] sexs = { };  // 因為int 類型的預設值是0. 所以當int[] 數組中沒有任何元素時。default value is 0; 如果有元素,則返回第一個元素15 16             //原方法: public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source);   // 擴充了IEnumerable<TSource>  介面17             //string namevalue = names.FirstOrDefault();  // string IEnumerable<string>.FirstOrDefault<string>();18             int sexvalue = sexs.FirstOrDefault(); // int IEnumerable<int>.FirstOrDefault<string>();19             //string namevalue = names.DefaultIfEmpty("QI").First();20             string namevalue = names.FirstOrDefault();21             Console.WriteLine("FirstOrDefault(): default(TSource) if source is empty; otherwise, the first element in source:{0}", namevalue);22 23 24         }25 26         /// <summary>27         /// 返回序列中的第一個元素。 28         /// 如果 source 中不包含任何元素,則 First<TSource>(IEnumerable<TSource>) 方法將引發異常29         /// ArgumentNullException      sourcevalue 為 null。30         //  InvalidOperationException  源序列為空白。31         /// </summary>32         public static void FunFirst()33         {34             //First()35             string[] names = { "Haiming QI", "Har", "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu", null };36             // string[] names = { };37 38             int[] sexs = { 100, 229, 44, 3, 2, 1 };39             //int[] sexs = { };40             int fsex = sexs.First();41             string fname = names.First(); // 如果序列中沒有元素則會發生,InvalidOperationException 異常。 源序列為空白。42 43             Console.WriteLine("First(): Returns the first element of a sequence : {0}", fname);44 45         }46         #endregion

以上是我在本地驗證的code.

需要注意的是:

 

這都是擴充了IEnumerable 這個介面。

public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source);

First 和 FirstOrDefault 最大的區別在於。 當集合(這個集合可以是:Arry,List,等等)中沒有元素的時候。 First 會報異常 InvalidOperationException 源序列為空白。
而 FirstOrDefault 則不會。

C# First and FirstOrDefault 方法詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.