Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 812 of 2109
Represent exact representation of no time in C#
The TimeSpan.Zero property in C# represents the exact representation of no time, which displays as 00:00:00. This is a static readonly field that provides a convenient way to initialize or compare TimeSpan values when you need to represent zero duration. Syntax Following is the syntax for using TimeSpan.Zero − TimeSpan timespan = TimeSpan.Zero; You can also use it for comparison operations − if (timespan == TimeSpan.Zero) { // timespan represents no time } Using TimeSpan.Zero for Initialization The most common use of TimeSpan.Zero is to initialize ...
Read MoreFormat TimeSpan in C#
The TimeSpan class in C# represents a time interval and provides several formatting options to display time in different formats. You can format a TimeSpan using standard format strings, custom format strings, or the ToString() method with format specifiers. Syntax Following is the syntax for formatting a TimeSpan using custom format strings − timeSpan.ToString("format_string") Following is the syntax for formatting using composite formatting − string.Format("{0:format_string}", timeSpan) Using Standard Format Strings TimeSpan supports standard format strings like "c" (constant format), "g" (general short), and "G" (general long) − ...
Read MoreStopwatch class in C#
The Stopwatch class in C# is used to measure elapsed time with high precision. It provides accurate timing measurements for performance monitoring and benchmarking applications. The class is found in the System.Diagnostics namespace. Syntax Following is the syntax for creating and starting a Stopwatch − Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Alternatively, you can create and start in one line − Stopwatch stopwatch = Stopwatch.StartNew(); Key Properties and Methods Property/Method Description Start() Starts measuring elapsed time Stop() Stops measuring elapsed ...
Read MoreC# Program to set the timer to zero
The Stopwatch class in C# provides precise timing capabilities. To set the timer to zero, you can use the Restart() method, which stops the current timer and immediately starts it again from zero. Syntax Following is the syntax to create and start a Stopwatch − Stopwatch stopwatch = Stopwatch.StartNew(); Following is the syntax to restart the timer and set it to zero − stopwatch.Restart(); Using Restart() Method The Restart() method combines two operations: it stops the current timer and immediately starts a new timing session from zero. This is ...
Read MoreC# Program to get the difference between two dates
The DateTime.Subtract() method in C# is used to calculate the difference between two dates. It returns a TimeSpan object representing the time interval between the two dates. Syntax Following is the syntax for using DateTime.Subtract() method − TimeSpan result = dateTime1.Subtract(dateTime2); Alternatively, you can use the subtraction operator − TimeSpan result = dateTime1 - dateTime2; Parameters The Subtract() takes one parameter − value − A DateTime object to subtract from the current instance. Return Value Returns a TimeSpan object that represents the difference between ...
Read MoreDisplay years in different formats with C# DateTime
In C#, you can display years in different formats using the DateTime.ToString() method with specific year format specifiers. The DateTime class provides several format specifiers to control how the year is displayed. Syntax Following is the syntax for formatting years using DateTime.ToString() − DateTime dt = DateTime.Now; dt.ToString("format_specifier"); Year Format Specifiers Format Specifier Description Example Output yy Two-digit year (00-99) 23 yyy Three-digit year with leading zeros 2023 yyyy Four-digit year 2023 yyyyy Five-digit year with leading zero 02023 ...
Read MoreUlong type in C#
The ulong type in C# is a 64-bit unsigned integer that represents the System.UInt64 structure. It can store values from 0 to 18, 446, 744, 073, 709, 551, 615, making it ideal for storing large positive numbers without sign considerations. The ulong keyword is an alias for System.UInt64 and is part of C#'s built-in value types. Since it's unsigned, it cannot store negative values but has twice the positive range compared to long. Syntax Following is the syntax for declaring a ulong variable − ulong variableName = value; You can also use the ...
Read MoreContainsKey in C#
The ContainsKey method in C# is a Dictionary method that checks whether a specified key exists in the dictionary. It returns a boolean value − true if the key is found, false otherwise. This method is essential for safe dictionary operations to avoid exceptions when accessing non-existent keys. Syntax Following is the syntax for the ContainsKey method − public bool ContainsKey(TKey key) Parameters key − The key to locate in the dictionary. This parameter cannot be null for reference types. Return Value Returns true if the dictionary ...
Read MoreConvert string to bool in C#
To convert a string to a bool in C#, you can use several methods. The most common approaches are bool.Parse(), bool.TryParse(), and Convert.ToBoolean(). Each method has different behaviors for handling invalid input. Syntax Following is the syntax for converting string to bool using different methods − bool result = bool.Parse(stringValue); bool result = Convert.ToBoolean(stringValue); bool.TryParse(stringValue, out bool result); Using bool.Parse() The bool.Parse() method converts a string representation of a boolean value to its boolean equivalent. It accepts "True" or "False" (case-insensitive) and throws an exception for invalid values − using System; ...
Read MoreC# program to convert string to long
Converting a string to a long data type in C# can be accomplished using several methods. The most common approaches are long.Parse(), Convert.ToInt64(), and long.TryParse() for safe conversion. Syntax Following are the different syntaxes for converting string to long − long result = long.Parse(stringValue); long result = Convert.ToInt64(stringValue); bool success = long.TryParse(stringValue, out long result); Using long.Parse() Method The long.Parse() method converts a string representation of a number to its 64-bit signed integer equivalent − using System; class Demo { static ...
Read More