Karthikeya Boyini has Published 2383 Articles

Get the creation time of a file in C#

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 14:43:02

2K+ Views

To get the creation time of a file in C#, use the CreationTime() 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;Let us see the complete code −Example Live Demousing System.IO; using System; public class Program { ... Read More

Literal number suffixes in C#

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 14:42:15

268 Views

To specify number types, use suffixes in C#. Literal number suffixes are numeric suffixes.For example, for long type −// long suffix long val1 = 29345L;For double type −// double suffix double val2 = 297.325D; Console.WriteLine(val2);Let us see more examples −Example Live Demousing System.IO; using System; public class Program {    public ... Read More

C# Program to skip initial elements in an array

karthikeya Boyini

karthikeya Boyini

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

2K+ Views

If you want to skip a number of elements in an array, then use the Skip() method in C#.Let’s say the following is our array −int[] arr = { 24, 40, 55, 62, 70, 82, 89, 93, 98 };Now to skip first four elements −var ele = arr.Skip(4);Let us see ... Read More

C# Program to find the largest element from an array using Lambda Expressions

karthikeya Boyini

karthikeya Boyini

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

467 Views

Declare an array −int[] arr = { 10, 90, 20, 19, 99, 57 };Now to get the largest element from an array, use Max() method with lambda expressions −arr.Max());Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] ... Read More

C# Program to combine Dictionary of two keys

karthikeya Boyini

karthikeya Boyini

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

659 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 ... Read More

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

karthikeya Boyini

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

C# Program to check whether a directory exists or not

karthikeya Boyini

karthikeya Boyini

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

186 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 ... Read More

C# Program to create new directories

karthikeya Boyini

karthikeya Boyini

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

107 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"); ... Read More

ToDictionary method in C#

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 14:28:32

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

C# program to Loop over a two dimensional array

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 14:13:58

4K+ Views

Declare a two dimensional array −string[, ] array = new string[3, 3];Set elements in the array −array[0, 0] = "One"; array[0, 1] = "Two"; array[0, 2] = "Three"; array[1, 0] = "Four"; array[1, 1] = "Five"; array[1, 2] = "Six"; array[2, 0] = "Seven"; array[2, 1] = "Eight"; array[2, 2] ... Read More

Advertisements