Found 34488 Articles for Programming

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

499 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);    } }

TimeSpan.From methods in C# ()

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

105 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

C# Program to display the number of days in a month

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

601 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

C# program to display the next day

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

2K+ 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 previous day

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

3K+ 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 Loop over a two dimensional array

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

4K+ 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

Advertisements