Found 34494 Articles for Programming

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

603 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

366 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

Represent exact representation of no time in C#

Arjun Thakur
Updated on 22-Jun-2020 14:10:56

41 Views

Use the TimeSpan.Zero to represent the exact representation of no time i.e −00:00:00Set an object for TimeSpan −TimeSpan ts;Now, the TimeSpan.Zero is used to display no time −TimeSpan ts = TimeSpan.Zero;Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       TimeSpan ts = TimeSpan.Zero;       Console.WriteLine(ts);    } }Output00:00:00

C# TicksPer constants

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

178 Views

TicksPer constants in C# have TicksPerDay, TicksPerHour, TicksPerMinute, TicksPerSecond, and TicksPerMillisecond constants. A millisecond consists of 10,000 ticks.Here are the TicksPer constants −TimeSpan.TicksPerDay TimeSpan.TicksPerHour TimeSpan.TicksPerMinuteExample Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       // TicksPer constant       Console.WriteLine(TimeSpan.TicksPerDay);       Console.WriteLine(TimeSpan.TicksPerHour);       Console.WriteLine(TimeSpan.TicksPerMinute);    } }Output864000000000 36000000000 600000000

C# Program to Add Two TimeSpan

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

496 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