- 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 34486 Articles for Programming
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
2K+ Views
Firstly, create a file using StreamWriter class and add content to it −using (StreamWriter sw = new StreamWriter("hello.txt")) { sw.WriteLine("This is demo line 1"); sw.WriteLine("This is demo line 2"); sw.WriteLine("This is demo line 3"); }Now use the ReadAllLines() method to read all the lines. With that, the Length property is to be used to get the count of lines −int count = File.ReadAllLines("hello.txt").Length;Here is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.IO; public class Program { public static void Main() { using (StreamWriter sw = new StreamWriter("hello.txt")) { ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
2K+ Views
To compare, ignoring case, use the case-insensitive Dictionary.While declaring a Dictionary, set the following property to get case-insensitive Dictionary −StringComparer.OrdinalIgnoreCaseAdd the property like this −Dictionary dict = new Dictionary (StringComparer.OrdinalIgnoreCase);Here is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program { public static void Main() { Dictionary dict = new Dictionary (StringComparer.OrdinalIgnoreCase); dict.Add("cricket", 1); dict.Add("football", 2); foreach (var val in dict) { Console.WriteLine(val.ToString()); } // case insensitive dictionary i.e. "cricket" is equal to "CRICKET" Console.WriteLine(dict["cricket"]); Console.WriteLine(dict["CRICKET"]); } }Output[cricket, 1] [football, 2] 1 1
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
662 Views
Firstly, set the Dictionaries to be combined −Dictionary dict1 = new Dictionary (); dict1.Add("one", 1); dict1.Add("Two", 2); Dictionary dict2 = new Dictionary (); dict2.Add("Three", 3); dict2.Add("Four", 4);Now, use HashSet to combine them. The method used for the same purpose is UnionWith() −HashSet hSet = new HashSet (dict1.Keys); hSet.UnionWith(dict2.Keys);The following is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program { public static void Main() { Dictionary dict1 = new Dictionary (); dict1.Add("one", 1); dict1.Add("Two", 2); Dictionary ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
71 Views
Use the ContainsValue() method in C# to search for a value in a Dictionary.Create a Dictionary and add elements −Dictionary d = new Dictionary(); d.Add("keyboard", "One"); d.Add("mouse", "Two");Now, use the ContainsValue() method to find a particular value −d.ContainsValue("Two");The following is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { Dictionary d = new Dictionary(); d.Add("keyboard", "One"); d.Add("mouse", "Two"); // search for a value Console.WriteLine(d.ContainsValue("Two")); } }OutputTrue
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
1K+ Views
The ToDictionary method is an extension method in C# and converts a collection into Dictionary.Firstly, create a string array −string[] str = new string[] {"Car", "Bus", "Bicycle"};Now, use the Dictionary method to convert a collection to Dictionary −str.ToDictionary(item => item, item => true);Here is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo { static void Main() { string[] str = new string[] {"Car", "Bus", "Bicycle"}; // key and value under ToDictionary var d = str.ToDictionary(item => item, item => true); foreach (var ele ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
166 Views
Set a list −List myList = new List() { 5, 10, 17, 19, 23, 33 };Let us say you need to find an element that is divisible by 2. For that, use the Find() method −int val = myList.Find(item => item % 2 == 0);Here is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo { static void Main() { List myList = new List() { 5, 10, 17, 19, 23, 33 }; Console.WriteLine("List: "); foreach(int i in myList) { Console.WriteLine(i); } int val = myList.Find(item => item % 2 == 0); Console.WriteLine("Element that divides by zero: "+val); } }OutputList: 5 10 17 19 23 33 Element that divides by zero: 10
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
5K+ Views
Use the Skip() method in C# to skip number of elements in an array.Let’s say the following is our array −int[] arr = { 10, 20, 30, 40, 50 };To skip the first two elements, use the Skip() method and add argument as 2 −arr.Skip(2);Let us see an example −Example Live Demousing System.IO; using System; using System.Linq; public class Demo { public static void Main() { int[] arr = { 10, 20, 30, 40, 50 }; Console.WriteLine("Initial Array..."); foreach (var res in arr) { Console.WriteLine(res); ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
240 Views
The All() extension method is part of the System.Linq namepspace. Using this method, you can check whether all the elements match a certain condition or not.Set an array −int[] arr = { 6, 7, 15, 40, 55 };The following is an example. It checks whether all the elements in the array is greater than and equal to 2 or not −arr.All(element => element > = 2);Here is the complete code −Example Live Demousing System; using System.Linq; class Program { static void Main() { int[] arr = { 6, 7, 15, 40, 55 }; bool res = arr.All(element => element >= 2); Console.WriteLine(res); } }OutputTrue
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
831 Views
Use the Aggregate method in C# to perform mathematical operations such as Sum, Min, Max, Average, etc.Let us see an example to multiply array elements using Aggregate method.Here is our array −int[] arr = { 10, 15, 20 };Now, use Aggregate() method −arr.Aggregate((x, y) => x * y);Here is the complete code −Example Live Demousing System; using System.Linq; using System.IO; public class Demo { public static void Main() { int[] arr = { 10, 15, 20 }; // Multiplication int res = arr.Aggregate((x, y) => x * y); Console.WriteLine(res); } }Output3000
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
377 Views
With the TakeWhile() method, you can get methods by setting a condition base on Predicate.Firstly, declare and initialize an array −int[] arr = { 25, 40, 65, 70};Now, use the TakeWhile() method and predicate to get all the elements that are less than 30.var val = arr.TakeWhile(ele => ele < 30);Let us see the same example, wherein we have displayed the values less than 30 using Predicate −Example Live Demousing System; using System.Linq; using System.IO; public class Demo { public static void Main() { int[] arr = { 25, 40, 65, 70}; var val = arr.TakeWhile(ele => ele < 30); foreach (int res in val) { Console.WriteLine(res); } } }Output25