![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display the default if element is not found in a C# List
We have a list without any element.
List<float> val = new List<float> { };
To display the default and avoid any error, use the FirstorDefault() method.
val.AsQueryable().FirstOrDefault();
You can also change the value to be displayed as default.
Let us see the code.
Example
using System; using System.Collections.Generic; using System.Linq; class Demo { static void Main() { List<float> val = new List<float>{ }; float a = val.AsQueryable().FirstOrDefault(); Console.WriteLine("Default Value = "+a); if (a == 0.0f) { a = 0.1f; } Console.WriteLine("Default Float value updated = "+a); } }
Output
Default Value = 0 Default Float value updated = 0.1
Advertisements