Element Operator-FirstOrDefault


The FirstOrDefault operator returns the first element of a sequence, or a default value if no element is found.


This sample uses FirstOrDefault to try to return the first element of the sequence, unless there are no elements, in which case the default value for that type is returned.



Example:


 








using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication4

{

class Program

{

  static void Main(string[] args)

   {

    int[] numbers = { };


    int firstNumOrDefault = numbers.FirstOrDefault();


    Console.WriteLine(firstNumOrDefault);


     Console.ReadLine();

    }

  }

}





Output:


                    

Aspnet Related Tutorials

...more

New Aspnet Resources

...more

Copyright © 2012 VisualBuilder. All rights reserved