George John has Published 1081 Articles

KeyValuePair in C#

George John

George John

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

12K+ Views

The KeyValuePair class stores a pair of values in a single list with C#.Set KeyValuePair and add elements −var myList = new List(); // adding elements myList.Add(new KeyValuePair("Laptop", 20)); myList.Add(new KeyValuePair("Desktop", 40)); myList.Add(new KeyValuePair("Tablet", 60));Here is the code to learn how to work with KeyValuePair and display the keys ... Read More

C# program to display the previous day

George John

George John

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

4K+ Views

To display the previous day, use the AddDays() method and a value -1 to get the previous day.Firstly, use the following to get the current day −DateTime.TodayNow, add -1 to the AddDays() method to get the previous day −DateTime.Today.AddDays(-1)The following is the code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; ... Read More

C# Program to Add Two TimeSpan

George John

George John

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

669 Views

Firstly, set two TimeSpans −TimeSpan t1 = TimeSpan.FromMinutes(1); TimeSpan t2 = TimeSpan.FromMinutes(2);To add it, use the Add() method −TimeSpan res = t1.Add(t2);ExampleUsing System; using System.Linq; public class Demo {    public static void Main() {       TimeSpan t1 = TimeSpan.FromMinutes(1);       TimeSpan t2 = TimeSpan.FromMinutes(2);   ... Read More

Combine two arrays in C#

George John

George John

Updated on 22-Jun-2020 13:56:18

5K+ Views

Firstly, declare and initialize two arrays −int[] arr1 = { 37, 45, 65 }; int[] arr2 = { 70, 89, 118 };Now create a new list −var myList = new List(); myList.AddRange(arr1); myList.AddRange(arr2);Use the AddRange() method the arrays into the newly created list.myList.AddRange(arr1); myList.AddRange(arr2);Now convert the list into array as ... Read More

Replace a C# array with a new array of different size

George John

George John

Updated on 22-Jun-2020 13:53:43

1K+ Views

To replace a C# aray with a new array, use the Array.Resize.Withing that, set the size of the new array −Array.Resize(ref arr, 4);Now add the new elements to the array as shown below −Example Live Demousing System; class Program {    static void Main() {       char[] arr = ... Read More

C# program to create an empty string array

George John

George John

Updated on 22-Jun-2020 13:51:23

4K+ Views

To create an empty string array −string[] str = new string[] {};Above, we haven’t added elements to the array, since it is empty.Even if we will loop though the array, it won’t display anything as shown below −Example Live Demousing System; public class Demo {    public static void Main() ... Read More

Bool Array in C#

George John

George John

Updated on 22-Jun-2020 13:49:25

9K+ Views

In a bool array, you can store true and false values. To set a bool array, use the new operator −bool[] arr = new bool[5];To add elements in the array −arr[0] = true; arr[1] = true; arr[2] = false; arr[3] = true; arr[4] = true;Let us see the complete code ... Read More

The nameof keyword in C#

George John

George John

Updated on 22-Jun-2020 13:47:39

554 Views

The nameof operator returns a string literal of an element that can be a variable, type or member.For example, the following is our variable −var vehicle = "motorbike";To get the string literal, use nameof −nameof(vehicle);The following is the code to implement nameof keyword −Example Live Demousing System; public class Program ... Read More

File Permissions in C#

George John

George John

Updated on 22-Jun-2020 13:21:18

3K+ Views

For File Permission in C#, use the FileIOPermission Class. It controls the ability to access files and folders.The following are the properties of File Permissions class −Sr.No.Methods & Description1AllFilesGets or sets the permitted access to all files.2AllLocalFilesGets or sets the permitted access to all local files.The following are the methods ... Read More

String slicing in C# to rotate a string

George John

George John

Updated on 22-Jun-2020 13:08:40

461 Views

Let’s say our string is −var str = "welcome";Use the substring() method and the following, if you want to rotate only some characters. Here, we are rotating only 2 characters −var res = str.Substring(1, str.Length - 1) + str.Substring(0, 2);The following is the complete code −Example Live Demousing System; public ... Read More

Advertisements