Samual Sam has Published 2310 Articles

ElementAt() method in C#

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:41:37

4K+ Views

ElementAt() is a System.Linq method in C# that is used to get and display element at a particular index.The following is our string array −string[] arr = { "One", "Two", "Three", "Four", "Five" };Now to get an element at index 0, use the ElementAt() method −arr.ElementAt(0);The following is the complete ... Read More

SkipWhile method in C#

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:40:31

359 Views

SkipWhile skips an element when a condition is matched.For example, use the following if you want to skip all even elements −ele => ele %2 == 0The following is an example wherein all the even elements are skipped and only the odd elements are displayed −Example Live Demousing System.IO; using System; ... Read More

C# Program to get the first three elements from a list

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:39:30

10K+ Views

Use the Take() method to get the first individual number of elements in C#.Firstly, set a list and add elements −List myList = new List(); myList.Add("One"); myList.Add("Two"); myList.Add("Three"); myList.Add("Four"); myList.Add("Five"); myList.Add("Six");Now, use the Take() method to get the elements from the list. Add the number of the elements you want ... Read More

ContainsValue in C#

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:38:44

111 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; ... Read More

Case-insensitive Dictionary in C#

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:37:38

3K+ 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() ... Read More

C# Program to write an array to a file

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:36:30

4K+ Views

Use WriteAllLines method to write an array to a file.Firstly, set a string array −string[] stringArray = new string[] {    "one",    "two",    "three" };Now, use the WriteAllLines method to add the above array to a file −File.WriteAllLines("new.txt", stringArray);Here is the complete code −Example Live Demousing System.IO; using System; ... Read More

C# Program to display the name of the directory

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:35:19

156 Views

Firstly, use the DirectoryInfo that operates on Directories. The parameter set in it is the file path −DirectoryInfo dir = new DirectoryInfo(@"D:ew\");To get the name of the directory, use the Name property −dir.NameThe following is the code to display the name of the directory −Example Live Demousing System.IO; using System; public ... Read More

C# Program to get information about a file

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:34:31

218 Views

To get information about a file means to get what all attributes are set for that particular file. For example, a file can be normal, hidden, archived, etc.Firstly, use the FileInfo class −FileInfo info = new FileInfo("hello.txt");Now, use FileAttributes to get information about a file −FileAttributes attr = info.Attributes;The following ... Read More

Find a specific element in a C# List

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:29:21

266 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 == ... Read More

Empty List in C#

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:14:20

9K+ Views

Set a list that has zero elements −List myList = new List();Now check whether the list is empty or null −Console.WriteLine(myList == null);Above, returns “False” i.e. the list is not null - the list is empty.Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class ... Read More

Advertisements