- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 2628 Articles for Csharp
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
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
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
367 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
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
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
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
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
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); } }
104 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