Csharp Articles

Page 102 of 196

ToDictionary method in C#

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 2K+ 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 −Exampleusing 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 in ...

Read More

ContainsValue in C#

Samual Sam
Samual Sam
Updated on 11-Mar-2026 140 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 −Exampleusing 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

Read More

Case-insensitive Dictionary in C#

Samual Sam
Samual Sam
Updated on 11-Mar-2026 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 −Exampleusing 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

Read More

C# Program to count the number of lines in a file

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 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 −Exampleusing System; using System.Collections.Generic; using System.IO; public class Program {    public static void Main() {       using (StreamWriter sw = new StreamWriter("hello.txt")) {         ...

Read More

C# Program to write an array to a file

Samual Sam
Samual Sam
Updated on 11-Mar-2026 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 −Exampleusing System.IO; using System; public class Program {    public static void Main() {       string[] stringArray = new string[] {          "one",          "two",          "three"       };       File.WriteAllLines("new.txt", stringArray);       using (StreamReader sr = new StreamReader("new.txt")) {          string res = sr.ReadToEnd();          Console.WriteLine(res);       }    } }Outputone two three

Read More

C# Program to check whether a directory exists or not

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 325 Views

Use the Directory. Exists method to check whether a directory exists or not.Let’s say you need to check whether the following directory exists or not −C:\AmitFor that, use the Exists() method −if (Directory.Exists("C:\Amit")) {    Console.WriteLine("Directory Amit Exist!"); }The following is the complete code −Exampleusing System.IO; using System; public class Program {    public static void Main() {       if (Directory.Exists("C:\Amit")) {          Console.WriteLine("Directory Amit Exist!");       } else {       Console.WriteLine("Directory Amit does not exist!");       }    } }OutputDirectory Amit does not exist!

Read More

C# Program to display the name of the directory

Samual Sam
Samual Sam
Updated on 11-Mar-2026 184 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 −Exampleusing System.IO; using System; public class Program {    public static void Main() {       DirectoryInfo dir = new DirectoryInfo(@"D:ew");       // displaying the name of the directory       Console.WriteLine(dir.Name);    } }OutputD:ew\

Read More

C# Program to get information about a file

Samual Sam
Samual Sam
Updated on 11-Mar-2026 258 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 is the code −Exampleusing System.IO; using System; public class Program {    public static void Main() {       using (StreamWriter sw = new StreamWriter("hello.txt")) {          sw.WriteLine("This is demo text!");       }       FileInfo info = new FileInfo("hello.txt");       FileAttributes attr = info.Attributes;       Console.WriteLine(attr);    } }OutputNormal

Read More

C# Program to get the last write time of a file

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 2K+ Views

To get the last write time of a file in C#, use the LastWriteTime() method.For this, use the FileInfo as well as DateTime classes.Create an object of each −FileInfo file = new FileInfo("amit.txt"); DateTime dt = file.CreationTime; dt = file.LastWriteTime;Let us see the complete code −Exampleusing System.IO; using System; public class Program {    public static void Main() {       using (StreamWriter sw = new StreamWriter("amit.txt")) {          sw.WriteLine("Welcome!");       }       FileInfo file = new FileInfo("amit.txt");       // file creation time       DateTime dt = file.CreationTime; ...

Read More

C# Program to get the last access time of a file

Samual Sam
Samual Sam
Updated on 11-Mar-2026 833 Views

To get the last access time of a file in C#, use the LastAccessTime() method.For this, use the FileInfo as well as DateTime classes.Create an object of each −FileInfo file = new FileInfo("new.txt"); DateTime dt = file.CreationTime; dt = file.LastAccessTime;Let us see the complete code −Exampleusing System.IO; using System; public class Program {    public static void Main() {       using (StreamWriter sw = new StreamWriter("quiz.txt")) {          sw.WriteLine("Quizzes!");       }       FileInfo file = new FileInfo("quiz.txt");       // last access time       DateTime dt = file.LastAccessTime;       Console.WriteLine(dt);    } }Output9/5/2018 5:21:43 AM

Read More
Showing 1011–1020 of 1,951 articles
« Prev 1 100 101 102 103 104 196 Next »
Advertisements