Found 34486 Articles for Programming

KeyValuePair in C#

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

10K+ 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 convert string to long

Chandu yadav
Updated on 22-Jun-2020 14:15:30

12K+ Views

To convert a string to a long, use the Long.parse method in C# −Firstly, set a string −string str = "6987646475767";Now, convert it to long −long.Parse(str);Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       string str = "6987646475767";       long res = long.Parse(str);       Console.WriteLine(res);    } }Output6987646475767

Convert string to bool in C#

Arjun Thakur
Updated on 22-Jun-2020 14:15:52

2K+ Views

To convert a string to a bool, use the Bool.parse method in C# −Firstly, set a string −string str = "false";Now, convert it to bool −bool.Parse(str);Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       string str = "false";       bool res = bool.Parse(str);       Console.WriteLine(res);    } }OutputFalse

ContainsKey in C#

Ankith Reddy
Updated on 22-Jun-2020 14:17:07

1K+ Views

ContainsKey is a Dictionary method in C# and check whether a key exists in the Dictionary or not.Declare a Dictionary and add elements −var dict = new Dictionary() {    {"TV", 1},    {"Home Theatre", 2},    {"Amazon Alexa", 3},    {"Google Home", 5},    {"Laptop", 5},    {"Bluetooth Speaker", 6} };Now, let’s say you need to check for the existence of a particular element in the Dictionary. For that, use the ContainsKey() method −if (dict.ContainsKey("Laptop") == true) {    Console.WriteLine(dict["Laptop"]); }The following is the code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() ... Read More

Ulong type in C#

George John
Updated on 22-Jun-2020 14:17:34

604 Views

The Ulong type in C# is an alias to the System.UInt64 type. It is an Unsigned integer.Set the Ulong type −ulong val = 7712059531;To get the minimum and maximum value for Ulong type −Console.WriteLine(ulong.MaxValue); Console.WriteLine(ulong.MinValue);Here is the complete code −Example Live Demousing System; using System.Threading; using System.Diagnostics; public class Demo {    public static void Main() {       ulong val = 7712059531;       Console.WriteLine(val);       // Max and Min values       Console.WriteLine(ulong.MaxValue);       Console.WriteLine(ulong.MinValue);       // typeof       Console.WriteLine(typeof(ulong));    } }Output7712059531 18446744073709551615 0 System.UInt64

Display years in different formats with C# DateTime

Chandu yadav
Updated on 22-Jun-2020 14:18:09

99 Views

Set a DateTime object;DateTime dt = DateTime.Now;To get years in different formats, try the following −dt.ToString("yy") dt.ToString("yyy") dt.ToString("yyyy") dt.ToString("yyyyy")Here is the complete code −Example Live Demousing System; using System.Threading; using System.Diagnostics; public class Demo {    public static void Main() {       DateTime dt = DateTime.Now;       // Today's date       Console.WriteLine("Today = {0}", DateTime.Today);       Console.WriteLine("Displaying current year in dufferent formats:");       Console.WriteLine(dt.ToString("yy"));       Console.WriteLine(dt.ToString("yyy"));       Console.WriteLine(dt.ToString("yyyy"));       Console.WriteLine(dt.ToString("yyyyy"));    } }OutputToday = 9/4/2018 12:00:00 AM Displaying current year in dufferent formats: 18 2018 2018 02018

C# Program to get the difference between two dates

Arjun Thakur
Updated on 22-Jun-2020 14:18:30

3K+ Views

Use DateTime.Subtract to get the difference between two dates in C#.Firstly, set two dates −DateTime date1 = new DateTime(2018, 8, 27); DateTime date2 = new DateTime(2018, 8, 28);Use the Subtract method to get the difference −TimeSpan t = date2.Subtract(date1);The following is the complete code −Example Live Demousing System; using System.Threading; using System.Diagnostics; public class Demo {    public static void Main() {       DateTime date1 = new DateTime(2018, 8, 27);       DateTime date2 = new DateTime(2018, 8, 28);       // getting the difference       TimeSpan t = date2.Subtract(date1);       Console.WriteLine(t);       Console.WriteLine("Days (Difference) = {0} ", t.TotalDays);       Console.WriteLine("Minutes (Difference) = {0}", t.TotalMinutes);    } }Output1.00:00:00 Days (Difference) = 1 Minutes (Difference) = 1440

C# Program to set the timer to zero

Ankith Reddy
Updated on 22-Jun-2020 14:18:51

369 Views

To set the timer information to zero, use the Stopwatch Restart method.Firstly, begin the Stopwatch −Stopwatch s = Stopwatch.StartNew();Then, use Restart() to set the timer to zero −s.Restart();Let us see the complete code −ExampleLive Demousing System; using System.Threading; using System.Diagnostics; public class Demo {    public static void Main() {       Stopwatch s = Stopwatch.StartNew();       Thread.Sleep(500);       // restart       s.Restart();       // begin again       Thread.Sleep(500);       Console.WriteLine(s.Elapsed);    } }Output00:00:00.5004937

Stopwatch class in C#

George John
Updated on 22-Jun-2020 14:19:14

2K+ Views

Stopwatch is a class in C# to measure the elapsed time. Use it to calculate the time a function took to execute. It is found under System.Diagnostics.To get the elapsed time, firstly begin the Stopwatch −var sw = Stopwatch.StartNew();For elapsed ticks −long ticks = sw.ElapsedTicks;Let us see an example −Example Live Demousing System; using System.Linq; using System.Diagnostics; public class Demo {    public static void Main() {       var sw = Stopwatch.StartNew();       long ticks = sw.ElapsedTicks;       Console.WriteLine(ticks);    } }Output582

Format TimeSpan in C#

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

2K+ Views

You can format a TimeSpan in the hh: mm: ss format in C#.Firstly, set the TimeSpan −TimeSpan ts = new TimeSpan(9, 15, 30);To format TimeSpan −{0:hh\:mm\:ss}The following is the code −Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       TimeSpan ts = new TimeSpan(9, 15, 30);       Console.WriteLine("{0:hh\:mm\:ss}", ts);    } }Output09:15:30

Advertisements