- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
- Related Articles
- Display only an element found in an array in MongoDB?
- Check if a list is not empty in MongoDB?
- How to check if Java list contains an element or not?
- Check if a doubly linked list of characters is palindrome or not in C++
- Insert default into not null column if value is null in MySQL?
- Check if every List element matches the predicate conditions in C#
- Cartilage is not found in(a)nose(b)ear(c)kidney(d)larynx
- How to Auto-Display a Default Value while Deleting a Value in a Drop-Down List in Excel?
- Which exception is raised when an element is not found in an HTML DOM using XPath in Selenium?
- Check if list is sorted or not in Python
- Python – Check if any list element is present in Tuple
- Check if a linked list is Circular Linked List in C++
- How to capture file not found exception in C#?
- Gmail login fail using Selenium webdriver. Showing element not found for password.
- List the essential vitamins found in food.

Advertisements