Found 34494 Articles for Programming

Long Time ("T") Format Specifier in C#

karthikeya Boyini
Updated on 23-Jun-2020 07:19:46

351 Views

The Long Time format specifier represents a custom date and time format string.It is defined by DateTimeFormatInfo.LongTimePattern property.The custom format string.HH:mm:ssExample Live Demousing System; using System.Globalization; class Demo {    static void Main() {       DateTime date = new DateTime(2018, 9, 9, 8, 15, 30);       Console.WriteLine(date.ToString("T", CultureInfo.CreateSpecificCulture("en-us")));    } }Output8:15:30 AM

C# Program to check a string for whitespace characters or null

Arjun Thakur
Updated on 23-Jun-2020 07:20:16

544 Views

This method returns true if the entered string has only whitespace characters or is null.Let’s say you are checking for whitespace character.bool val1 = string.IsNullOrWhiteSpace(“ “);The above returns True, since the string is a whitespace character. In the same way, check it for null using the IsNullOrWhiteSpace() method.Here is the entire example that checks for null and whitespace −Exampleusing System; using System.IO; public class Demo {    public static void Main() {       bool val1 = string.IsNullOrWhiteSpace(“100”);       bool val2 = string.IsNullOrWhiteSpace(" ");       bool val3 = string.IsNullOrWhiteSpace(null);       Console.WriteLine(val1);       Console.WriteLine(val2);       Console.WriteLine(val3);    } }

IsNullOrWhiteSpace() Method in C#

Samual Sam
Updated on 23-Jun-2020 07:10:07

2K+ Views

This method returns true if the entered string has only whitespace characters or is null.Let say you checked for null value −bool val1 = string.IsNullOrWhiteSpace(null);The above returns True, since the string is “null”. In the same way, check it for whitespace character.Here is the entire example that checks for null and whitespace.Example Live Demousing System; using System.IO; public class Demo {    public static void Main() {       bool val1 = string.IsNullOrWhiteSpace(null);       bool val2 = string.IsNullOrWhiteSpace(" ");       bool val3 = string.IsNullOrWhiteSpace("5");       Console.WriteLine(val1);       Console.WriteLine(val2);       Console.WriteLine(val3);    } }OutputTrue True False

C# Program to add a node at the first position in a Linked List

Chandu yadav
Updated on 23-Jun-2020 07:10:46

332 Views

Firstly, set a LinkedList with nodes.string [] students = {"Tim", "Jack", "Henry", "David", "Tom"}; LinkedList list = new LinkedList(students);To add a node at the first position, use the AddFirst() method.list.AddFirst("Amit");Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       string [] students = {"Tim", "Jack", "Henry", "David", "Tom"};       LinkedList list = new LinkedList(students);       foreach (var stu in list) {          Console.WriteLine(stu);       }       // adding a node       Console.WriteLine("LinkedList after adding a node at the first position...");   ... Read More

C# Program to read all the lines of a file at once

karthikeya Boyini
Updated on 10-Apr-2020 09:42:53

144 Views

Use ReadAllText() method to read all the lines of a file at once.Let’s say we have a file “hello.txt” with the following lines −One Two ThreeTo read the above file, add the path of the file as parameter.File.ReadAllText(myPath);Above, myPath had the file path.String myPath = "hello.txt";Let us see the complete code −Exampleusing System; using System.IO; public class Demo {    public static void Main() {       String myPath = "hello.txt";       String allLines;       allLines = File.ReadAllText(myPath);       Console.WriteLine(allLines);    } }OutputThe following is the output −One Two Three

The "E" and "e" custom specifiers in C#

George John
Updated on 23-Jun-2020 07:11:31

297 Views

If any of the following string appears in the format string and followed by at least one zero, then the number is formatted with an “E” or “e” in between the number and the exponent."E" "E+" "E-" "e" "e+" "e-"Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       double num = 9400;       Console.WriteLine(num.ToString("0.###E+0", CultureInfo.InvariantCulture));       Console.WriteLine(String.Format(CultureInfo.InvariantCulture, "{0:0.###E+0}", num));       Console.WriteLine(num.ToString("0.###E+000", CultureInfo.InvariantCulture));       Console.WriteLine(String.Format(CultureInfo.InvariantCulture, "{0:0.###E+000}", num));    } }Output9.4E+3 9.4E+3 9.4E+003 9.4E+003

Delete a file in C#

Ankith Reddy
Updated on 23-Jun-2020 07:12:15

711 Views

Use File.Delete method to delete a file.Firstly, set the path of the file you want to delete.String myPath = @"C:\New\amit.txt";Now, use the File.Delete method to delete the file.File.Delete(myPath);The following is the complete code −Example Live Demousing System; using System.IO; public class Program {    public static void Main() {       String myPath = @"C:\New\amit.txt";       Console.WriteLine("Deleting File");       File.Delete(myPath);    } }OutputDeleting File

C# Program to make a copy of an existing file

Samual Sam
Updated on 23-Jun-2020 07:11:53

754 Views

Use File.Copy method to make copy of an existing file.Add the path of the file you want to copy.String myPath = @"D:\one.txt";Now copy the above file to the following file −String myPath = @"D:\one.txt";Use the File.Copy method with both the source and destination file.File.Copy(myPath,newpath);Exampleusing System; using System.IO; public class Program {    public static void Main() {       String myPath = @"D:\one.txt";       // the file will get copied here       String newpath = @"D:\two.txt";       // copying file       File.Copy(myPath,newpath);    } }

C# Linq Where Method

karthikeya Boyini
Updated on 23-Jun-2020 07:13:44

2K+ Views

The Where method filters an array of values based on a predicate.Here, the predicate is checking for elements above 70.Where((n, index) => n >= 70);Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] arr = { 10, 30, 20, 15, 90, 85, 40, 75 };       Console.WriteLine("Array:");       foreach (int a in arr)       Console.WriteLine(a);       // getting elements above 70       IEnumerable myQuery = arr.AsQueryable().Where((n, index) => n >= 70);       Console.WriteLine("Elements above 70...:");       foreach (int res in myQuery)       Console.WriteLine(res);    } }OutputArray: 10 30 20 15 90 85 40 75 Elements above 70...: 90 85 75

C# OfType() Method

Arjun Thakur
Updated on 23-Jun-2020 07:12:57

472 Views

Filter a collection on the basis of each of its elements type.Let’s say you have the following list with integer and string elements −list.Add("Katie"); list.Add(100); list.Add(200);To filter collection and get only elements with string type.var myStr = from a in list.OfType() select a;Work the same for integer type.var myInt = from a in list.OfType() select a;The following is the complete code −Example Live Demousing System; using System.Linq; using System.Collections; public class Demo {    public static void Main() {       IList list = new ArrayList();       list.Add("Katie");       list.Add(100);       list.Add(200);     ... Read More

Advertisements