Example # 1
Here, we have an instance in which we use the “FirstOrDefault” method. You will check in this instance how this method works in C# programming. We will print both values in this example. The first value of the sequence contains values and also the default value of the empty sequence. We must save our C# program file with the “.cs” file extension. We can save the file with any file name of our choice but must have a “.cs” file extension.
In C# programming, we must start our code with the C# library, which is “using System” here. We get access to classes like the “Console” class and also the function as the “WriteLine” function with the help of this C# library. After using this C# library, we have “System.Linq” which we use in the C# program to provide us with various classes and methods that support LINQ queries. We also have a “System.Collections.Generic” namespace, which is here for interfaces and classes. It provides good performance.
Then we have a class, and the name of the class is “Program” in this C# code. After creating the class, we have to invoke the “main” function. We invoked our “main” function by using the statement “static public void Main ()”. Now, we are going to form an array named “seq1”. This array is of integer data type. We can pass integers to this array. We store different integer values in this integer array. We store “122, 33, 55, 77, 243, 65” in this array of “seq1”. We also declare a variable “var” named “result1” and initialize it with the “FirstOrDefault” method. We use this method with “seq1” for getting the value of the “seq1” whose sequence is less than “77”.
The FirstOrDefault method will check the values of “seq1” that are less than “77” and give the first value as a result and store this value in “var result1”. We can write this condition as “(sequence => sequence < 77)”. The “seq1.FirstOrDefault” means we apply this method on “seq1”. We print this “result1” value by using the “Console.WriteLine()” statement. We create another array with the name “seq2” which is again the integer data type. But this time, the array is empty. We don’t save any value in this “seq2”. This “seq2” is empty here, so we can get the default value when using the “FirstOrDefault” method. This will not throw an exception here.
We declare a new variable here named “result2” which shows the result of this “seq2”. We initialize this “result2” with the “seq2.FirstOrDefault” method, but in this case, the “seq2” is empty, so it will store the default value in “result2”. After this, we print this “result2” by using the same function “Console.WriteLine”.
We also provide you with the output of this code to understand how this code works. As we are performing these examples in Ubuntu 20.04 so, when we want to get the output in Ubuntu 20.04, we have to use some commands here. We have two commands for getting the output. One is the “mcs” command used for compilation, and the other is the “mono” command used to execute our C# code. The “mcs” command is the compilation command, so we use the “.cs” extension.
The output shows that it prints the first value of “seq1” which is less than “77” and the default value of “seq2” because “seq2” is empty. The default value of “seq2” is “0” here, and it prints “0” on the terminal screen. It doesn’t throw an exception because we use the “FirstOrDefault” method.
Example # 2
Now, explore another example here. We will put on some conditions and use the “FirstOrDefault” method in this instance.
The first statement is “using System” which means we use the C# library. Then we have “System.Linq” and “System.Collections.Generic” which we discussed in our first example. We have a class named “Demo” in this C# code and invoke our “main” method inside this. We are going to create four different lists in this code. First, we create the list of integers with the name “List1” and store some integer data in this “List1”. Here “int” represents integer data type. We store “8, 11, 22, 37, 58, 97” to this first list of integers, “List1”.
After this first list, we create another list with the name “List2” and this “List2” is of “string” data type. We store the “string” data in this “List2”. We store “Apple”, “Orange”, and “Banana” in “List2”. The third list is named “List3” and this “List3” is also of “string” data type. We store the first element, “null” and other string elements “one”, “two”, and “three” in this “List3”. After these three lists, we create our last string list with the name “List4” and this is an empty list. We get the first element of “List1” using the “FirstOrDefault” method and print this value using “Console.WriteLine”.
We will apply a condition on this “List1” in the “FirstOrDefault” method. We write the condition that it gets the value whose remainder is “0” when it divides by 2. So, this value is the even value. The “FirstOrDefault” method stores the first value, which is even in this “List1”. In the same way, we get the first string of “List2” using “FirstOrDefault”. We use this “FirstOrDefault” method with “List3” and “List4” also. The output shows how this “FirstOrDefault” method gets the first or default values or strings.
In the first line, it prints the first value of the “List1”. After this, it prints the even value of the same list, “List1”. The next line prints nothing in the place of the first value because the 1st element in “List3” is “null”. And as you know, our last list is empty, so the 1st value of the “List4” is also empty. There is no exception because the “FirstOrDefault” method handles empty lists or “null” values.
Conclusion
We provide you with this guide to easily learn how this “FirstOrDefault” method is used in C# programming in Ubuntu 20.04. In this guide, we have explained this method in detail and explored examples in which we have used this method, and showed you how this method deals with “null” values and empty lists or arrays. This method doesn’t throw an exception for empty or null values or elements. We have explained the code and also given the code. We paste the output into this guide, so it’s easy for you to learn from this guide about this method. I hope you will learn from this guide and use this concept in your C# program.
from https://ift.tt/GJQ3LWx
0 Comments