Key-Value Pair in C#

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 and values −ExampleUsing System; using System.Collections.Generic; class Program {    static void Main() {       var myList = new List();       // adding elements       myList.Add(new KeyValuePair("Laptop", 20));       myList.Add(new KeyValuePair("Desktop", 40));       myList.Add(new KeyValuePair("Tablet", 60));       foreach (var val in myList) {          Console.WriteLine(val);       }    } }

C# Program to Find the Smallest Element from an Array

Ankith Reddy
Updated on 22-Jun-2020 14:14:43

2K+ Views

Declare an array −int[] arr = { 5, 9, 2, 7 };Now to get the smallest element from an array, use the Min() method −arr.Min());Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] arr = { 5, 9, 2, 7 };       Console.WriteLine(arr.Min());    } }Output2

Empty List in C#

Samual Sam
Updated on 22-Jun-2020 14:14:20

9K+ Views

Set a list that has zero elements −List myList = new List();Now check whether the list is empty or null −Console.WriteLine(myList == null);Above, returns “False” i.e. the list is not null - the list is empty.Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List myList = new List();       // returns false i.e. an empty list (not a null list)       Console.WriteLine(myList == null);    } }OutputFalse

C# Program to Loop Over a Two-Dimensional Array

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

5K+ 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] = "Nine";Now, get the upper bound to get the dimensions to loop over the array −int uBound0 = array.GetUpperBound(0); int uBound1 = array.GetUpperBound(1);Iterate through a nested loop, until the above two values as shown in the code below −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo {   ... Read More

C# Program to Display the Previous Day

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; public class Demo {    public static void Main() {       Console.WriteLine("Today = {0}", DateTime.Today);       Console.WriteLine("Previous Day = {0}", DateTime.Today.AddDays(-1));    } }OutputToday = 9/4/2018 12:00:00 AM Previous Day = 9/3/2018 12:00:00 AM

C# Program to Display the Next Day

Ankith Reddy
Updated on 22-Jun-2020 14:12:50

3K+ Views

To display the next day, use the AddDays() method and a value +1 to get the next day.Firstly, use the following to get the current day −DateTime.TodayNow, add 1 to the AddDays() method to get the next day −DateTime.Today.AddDays(1)The following is the code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       Console.WriteLine("Today = {0}", DateTime.Today);       Console.WriteLine("Previous Day = {0}", DateTime.Today.AddDays(-1));       Console.WriteLine("Next Day (Tomorrow) = {0}", DateTime.Today.AddDays(1));    } }OutputToday = 9/4/2018 12:00:00 AM Previous Day = 9/3/2018 12:00:00 AM Next Day (Tomorrow) = 9/5/2018 12:00:00 AM

C# Program to Display the Number of Days in a Month

Arjun Thakur
Updated on 22-Jun-2020 14:12:25

765 Views

Use the DaysInMonth to display the number of days in a month.Add the year and month as a parameter for the DaysInMonth() method −DateTime.DaysInMonth(2018, 8);The following is an example −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       Console.WriteLine("Today = {0}", DateTime.Today);       int num_days = DateTime.DaysInMonth(2018, 8);       Console.WriteLine("Days in August: "+num_days);    } }OutputToday = 9/4/2018 12:00:00 AM Days in August: 31

Timespan From Methods in C#

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

205 Views

The TimeSpan.From methods include FromDays, FromHours, FromMinutes, etc.To get the TimeSpan for all the methods// Days TimeSpan t1 = TimeSpan.FromDays(1); // Hours TimeSpan t2 = TimeSpan.FromHours(1); // Minutes TimeSpan t3 = TimeSpan.FromMinutes(1);The following is the code that works on all the TimeSpan methods −Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       TimeSpan t1 = TimeSpan.FromDays(1);       TimeSpan t2 = TimeSpan.FromHours(1);       TimeSpan t3 = TimeSpan.FromMinutes(1);       Console.WriteLine(t1);       Console.WriteLine(t2);       Console.WriteLine(t3);    } }Output1.00:00:00 01:00:00 00:01:00

Generate Days from Range of Dates Using MySQL Views

karthikeya Boyini
Updated on 22-Jun-2020 14:11:56

363 Views

To illustrate it we are creating the following views −mysql> CREATE VIEW digits AS     -> SELECT 0 AS digit UNION ALL     -> SELECT 1 UNION ALL     -> SELECT 2 UNION ALL     -> SELECT 3 UNION ALL     -> SELECT 4 UNION ALL     -> SELECT 5 UNION ALL     -> SELECT 6 UNION ALL     -> SELECT 7 UNION ALL     -> SELECT 8 UNION ALL     -> SELECT 9; Query OK, 0 rows affected (0.08 sec) mysql> CREATE VIEW numbers AS SELECT ones.digit + ... Read More

C# Program to Add Two TimeSpan

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

702 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);       Console.WriteLine("First TimeSpan: "+t1);       Console.WriteLine("Second TimeSpan: "+t2);       // Adding       TimeSpan res = t1.Add(t2);       Console.WriteLine("Resultant TimeSpan: "+res);    } }

Advertisements