- Trending Categories
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
Found 34489 Articles for Programming
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
337 Views
To handle empty collections, use the DefaultIfEmpty() method in C#.If an array is empty, then using this method will show the default method instead of displaying an error.Let’s say we have an empty list.List myList = new List();Now, use DefaultIfEmpty() method to display the default value.myList.DefaultIfEmpty();Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo { static void Main() { List myList = new List(); var res = myList.DefaultIfEmpty(); foreach (var a in res) { Console.WriteLine(a); } } }Output0
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
231 Views
Set a sequence and add elements.List ID = new List { 120, 111, 250, 111, 120, 300, 399, 450 };Use Distinct() method to get distinct element from the above list.IEnumerable res = ID.AsQueryable().Distinct();Let us see the complete code.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo { static void Main() { List ID = new List { 120, 111, 250, 111, 120, 300, 399, 450 }; // get distinct elements IEnumerable res = ID.AsQueryable().Distinct(); foreach (int arr in res) { Console.WriteLine(arr); } } }Output120 111 250 300 399 450
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
3K+ Views
The following is our Dictionary with some elements −Dictionary d = new Dictionary() { {1,"Electronics"}, {2, "Clothing"}, {3,"Toys"}, {4,"Footwear"}, {5, "Accessories"} };Now to display the first element, set the key like this.d[1];The above displays the first element.Example Live Demousing System; using System.Collections.Generic; public class Program { public static void Main() { Dictionary d = new Dictionary() { {1,"Electronics"}, {2, "Clothing"}, {3,"Toys"}, {4,"Footwear"}, {5, "Accessories"} }; foreach (KeyValuePair ele in d) { Console.WriteLine("Key = {0}, Value = {1}", ele.Key, ele.Value); } Console.WriteLine("First element: "+d[1]); } }OutputKey = 1, Value = Electronics Key = 2, Value = Clothing Key = 3, Value = Toys Key = 4, Value = Footwear Key = 5, Value = Accessories First element: Electronics
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
25K+ Views
To add key-value pair in C# Dictionary, firstly declare a Dictionary.IDictionary d = new Dictionary();Now, add elements with KeyValuePair.d.Add(new KeyValuePair(1, "TVs")); d.Add(new KeyValuePair(2, "Appliances")); d.Add(new KeyValuePair(3, "Mobile"));After adding elements, let us display the key-value pair.Example Live Demousing System; using System.Collections.Generic; public class Demo { public static void Main() { IDictionary d = new Dictionary(); d.Add(new KeyValuePair(1, "TVs")); d.Add(new KeyValuePair(2, "Appliances")); d.Add(new KeyValuePair(3, "Mobile")); d.Add(new KeyValuePair(4, "Tablet")); d.Add(new KeyValuePair(5, "Laptop")); d.Add(new KeyValuePair(6, "Desktop")); d.Add(new KeyValuePair(7, "Hard Drive")); ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
383 Views
Clear a Hashtable, using the Clear() method in C#.The following is our Hashtable −Hashtable h = new Hashtable(); h.Add(1, "Amit"); h.Add(2, "Sachin"); h.Add(3, "Rahul");Use the clear method.h.Clear();If you will now try to display the Hashtable, nothing would get display since the Hashtable is empty.Example Live Demousing System; using System.Collections; public class Demo { public static void Main() { Hashtable h = new Hashtable(); h.Add(1, "Amit"); h.Add(2, "Sachin"); h.Add(3, "Rahul"); Console.WriteLine("Keys and Values list:"); foreach (var key in h.Keys ) { ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
248 Views
Set a Hashtable collection and add some elements to it.Hashtable h = new Hashtable(); h.Add(1, "Sam"); h.Add(2, "Jack"); h.Add(3, "Andy"); h.Add(4, "Katie"); h.Add(5, "Beth"); h.Add(6, "Benjamin");Use the ContainsKey() method to check whether a key exists in a Hashtable or not.Let’s check for key 3. It returns True of the key is found.h.ContainsKey(3));Example Live Demousing System; using System.Collections; public class Demo { public static void Main() { Hashtable h = new Hashtable(); h.Add(1, "Sam"); h.Add(2, "Jack"); h.Add(3, "Andy"); h.Add(4, "Katie"); h.Add(5, "Beth"); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
8K+ Views
To convert a specified value to a double-precision floating-point number, use Convert.ToDouble() method.The following is our long value −long[] val = { 340, -200};Now convert it to Double.double result; result = Convert.ToDouble(val);Example Live Demousing System; public class Demo { public static void Main() { long[] val = { 340, -200}; double result; // long to double foreach (long number in val) { result = Convert.ToDouble(number); Console.WriteLine("Converted {0} value to {1}",number, result); } } }OutputConverted 340 value to 340 Converted -200 value to -200
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
231 Views
Set Hahtable collection with elements.Hashtable h = new Hashtable(); h.Add(1, "Jack"); h.Add(2, "Henry"); h.Add(3, "Ben"); h.Add(4, "Chris");Let’s say now you need to find a value, then use the ContainsValue() method.We are finding value “Chris” here −h.ContainsValue(“Chris”);Example Live Demousing System; using System.Collections; public class Demo { public static void Main() { Hashtable h = new Hashtable(); h.Add(1, "Jack"); h.Add(2, "Henry"); h.Add(3, "Ben"); h.Add(4, "Chris"); Console.WriteLine("Keys and Values list:"); foreach (var key in h.Keys ) { Console.WriteLine("Key ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
415 Views
Set Hashtable collection with elements.Hashtable h = new Hashtable(); h.Add(1, "Jack"); h.Add(2, "Henry"); h.Add(3, "Ben"); h.Add(4, "Chris");Let’s say now you need to find any key, then use the Contains() method. We are finding key 3 here −h.Contains(3);The following is the complete example.Example Live Demousing System; using System.Collections; public class Demo { public static void Main() { Hashtable h = new Hashtable(); h.Add(1, "Jack"); h.Add(2, "Henry"); h.Add(3, "Ben"); h.Add(4, "Chris"); Console.WriteLine("Keys and Values list:"); foreach (var key in h.Keys ) ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
617 Views
The following is our Hashtable −Hashtable h = new Hashtable(); h.Add(1, "Jack"); h.Add(2, "Henry"); h.Add(3, "Ben"); h.Add(4, "Chris");To remove an item, use the Remove() method. Here, we are removing the 3rd element.h.Remove(3);Let us see the complete example.Example Live Demousing System; using System.Collections; public class Demo { public static void Main() { Hashtable h = new Hashtable(); h.Add(1, "Jack"); h.Add(2, "Henry"); h.Add(3, "Ben"); h.Add(4, "Chris"); Console.WriteLine("Initial list:"); foreach (var key in h.Keys ) { Console.WriteLine("Key = ... Read More