Case Insensitive Dictionary in C#

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() {       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

C# Program to Count the Number of Lines in a File

karthikeya Boyini
Updated on 22-Jun-2020 14:37:06

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

C# Program to Write an Array to a File

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; 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

Multiple Stored Generated Columns in MySQL Table

Chandu yadav
Updated on 22-Jun-2020 14:36:03

166 Views

It is quite possible to add multiple stored generated columns in a MySQL table. It can be illustrated with the following example as follows −Examplemysql> Create table profit1(cost int, price int, profit int AS (price-cost) STORED, price_revised int AS (price-2) STORED); Query OK, 0 rows affected (0.36 sec) mysql> Describe profit1; +---------------+---------+------+-----+---------+------------------+ | Field         | Type    | Null | Key | Default | Extra            | +---------------+---------+------+-----+---------+------------------+ | cost          | int(11) | YES  |     | NULL    |             ... Read More

Check If a Directory Exists in C#

karthikeya Boyini
Updated on 22-Jun-2020 14:35:49

291 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 −Example Live Demousing 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!

Multiple Virtual Generated Columns in MySQL Table with CREATE TABLE Statement

Paul Richard
Updated on 22-Jun-2020 14:35:35

301 Views

It is quite possible to add multiple virtual generated columns in a MySQL table. It can be illustrated with the following example as follows −Examplemysql> Create table profit(cost int, price int, profit int AS (price-cost), price_revised int AS (price-2)); Query OK, 0 rows affected (0.73 sec) mysql> Describe profit; +---------------+---------+------+-----+---------+-------------------+ | Field         | Type    | Null | Key | Default | Extra             | +---------------+---------+------+-----+---------+-------------------+ | cost          | int(11) | YES  |     | NULL    |               ... Read More

C# Program to Display the Name of the Directory

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

162 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 class Program {    public static void Main() {       DirectoryInfo dir = new DirectoryInfo(@"D:ew\");       // displaying the name of the directory       Console.WriteLine(dir.Name);    } }OutputD:ew\

Alter Table to Add MySQL Stored GENERATED COLUMNS

Sharon Christine
Updated on 22-Jun-2020 14:35:06

229 Views

For adding MySQL stored GENERATED COLUMNS in a table, we can use the same syntax as adding a column just adding “AS(expression)” after the data type. Its syntax would be as follows −SyntaxALTER TABLE table_name ADD COLUMN column_name AS(expression)STORED;Examplemysql> ALTER TABLE employee_data_stored ADD COLUMN FULLName Varchar(200) AS (CONCAT_WS(" ", 'First_name', 'Last_name')) STORED; Query OK, 2 rows affected (1.23 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> Describe employee_data_stored; +------------+--------------+------+-----+---------+------------------+ | Field      | Type         | Null | Key | Default | Extra            | +------------+--------------+------+-----+---------+------------------+ | ID       ... Read More

C# Program to Create New Directories

karthikeya Boyini
Updated on 22-Jun-2020 14:34:54

173 Views

Use the CreateDirectory method to create a directory.Let’s say you need to create a directory in D drive. For that, use the CreateDirectory() method as shown below −Directory.CreateDirectory("D:\Tutorial");The following is the code −Exampleusing System.IO; using System; public class Program {    public static void Main() {       Directory.CreateDirectory("D:\Tutorial");    } }

C# Program to Get Information About a File

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

226 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 −Example Live Demousing 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

Advertisements