
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 2587 Articles for Csharp

2K+ Views
The method matches instances of a pattern and is used to extract value based on a pattern.Let us see hoe to check for a valid URL.For that, pass the regex expression in the Matches method.MatchCollection mc = Regex.Matches(text, expr);Above, the expr is our expression that we have set to check for valid URL."^(http|http(s)?://)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?”The text we have set to check is a URL i.e.https://demo.comLet us see the complete code.Example Live Demousing System; using System.Text.RegularExpressions; namespace Demo { class Program { private static void showMatch(string text, string expr) { MatchCollection mc = Regex.Matches(text, expr); ... Read More

20K+ Views
Use the FirstorDefault() method to return the first element of a sequence or a default value if element isn’t there.The following is our empty list −List val = new List { };Now, we cannot display the first element, since it is an empty collection. For that, use the FirstorDefault() method to display the default value.val.AsQueryable().FirstOrDefault();The following is the complete example.Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo { static void Main() { List val = new List { }; double d = val.AsQueryable().FirstOrDefault(); Console.WriteLine("Default Value = "+d); ... Read More

598 Views
Find common elements between two arrays using the Intersect() method.The following are our arrays −int[] val1 = { 15, 20, 40, 60, 75, 90 }; int[] val2 = { 17, 25, 35, 55, 75, 90 };To perform intersection.val1.AsQueryable().Intersect(val2);Let us see the entire example.Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo { static void Main() { int[] val1 = { 15, 20, 40, 60, 75, 90 }; int[] val2 = { 17, 25, 35, 55, 75, 90 }; IEnumerable res = val1.AsQueryable().Intersect(val2); Console.WriteLine("Intersection of both the lists..."); foreach (int a in res) Console.WriteLine(a); } }OutputIntersection of both the lists... 75 90

5K+ Views
To cast a specific type to its IEnumerable equivalent, use the AsEnumerable() method. It is an extension method.The following is our array −int[] arr = new int[5]; arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50;Now, get the IEnumerable equivalent.arr.AsEnumerable();Example Live Demousing System; using System.Linq; class Demo { static void Main() { int[] arr = new int[5]; arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50; var res = arr.AsEnumerable(); foreach (var ele in res) { Console.WriteLine(ele); } } }Output10 20 30 40 50

3K+ Views
The following are our strings in a list −List list = new List { "keyboard", "mouse", "joystick", "monitor" };To use the first 3 letters, use substring method and use it under the Linq Select method.IEnumerable res = list.AsQueryable() .Cast() .Select(str => str.Substring(0, 3));Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo { static void Main() { List list = new List { "keyboard", "mouse", "joystick", "monitor" }; // getting first 3 letters from every string IEnumerable res = list.AsQueryable() .Cast() .Select(str => str.Substring(0,3)); foreach (string str in res) { Console.WriteLine(str); } } }Outputkey mou joy mon

426 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

290 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

5K+ 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

29K+ 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

462 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