TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# ElementAt, ElementAtOrDefault Use

Use the ElementAtOrDefault and ElementAt methods. Add the System.Linq namespace.
ElementAtOrDefault. ElementAtOrDefault accesses an element. It handles an out-of-range access without throwing an exception. It returns a default value in this case. ElementAt meanwhile will throw an error on nonexistent indexes.LINQ
Example. In this program we create a three-element array. The only valid indexes to access are 0, 1 and 2. We show how ElementAtOrDefault works on valid indexes, and also out-of-range indexes.

Tip: On out-of-range accesses, ElementAtOrDefault returns the default value for the type, which for int is 0.

Default
C# program that uses ElementAtOrDefault using System; using System.Linq; class Program { static void Main() { int[] array = { 4, 5, 6 }; int a = array.ElementAtOrDefault(0); int b = array.ElementAtOrDefault(1); int c = array.ElementAtOrDefault(-1); int d = array.ElementAtOrDefault(1000); Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); Console.WriteLine(d); } } Output 4 5 0 0
Conceptually, ElementAtOrDefault can make a collection be infinite. It can make every possible index returning a valid value—the default value—if it is not actually present in memory.

And: This could be useful if a collection is queried for invalid indexes, and actual stored values are not necessary for the algorithm.

ElementAt. ElementAt gets an element at an index. In many IEnumerable types, you cannot directly index a certain element. Instead of using a foreach-loop or other techniques, we can use the ElementAt method from System.Linq.

To start: This program uses an array of three string literal elements. Arrays are IEnumerable collections.

ArrayIEnumerable

And: We can index this array through the ElementAt method in the System.Linq namespace.

Warning: The method call with argument 3 throws a System.ArgumentOutOfRangeException.

ArgumentException
C# program that uses ElementAt method using System; using System.Linq; class Program { static void Main() { // Input array. string[] array = { "Dot", "Net", "Perls" }; // Test ElementAt for 0, 1, 2. string a = array.ElementAt(0); Console.WriteLine(a); string b = array.ElementAt(1); Console.WriteLine(b); string c = array.ElementAt(2); Console.WriteLine(c); // This is out of range. string d = array.ElementAt(3); } } Output Dot Net Perls Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range.
Summary. These methods can transform how we access collections. ElementAtOrDefault eliminates the need for bounds-checking. But it could create extra complexity if we need to add special casing for the default value.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf