Csharp Articles

Page 137 of 196

Stopwatch class in C#

George John
George John
Updated on 17-Mar-2026 2K+ Views

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 More

C# Program to set the timer to zero

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 512 Views

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 More

C# Program to get the difference between two dates

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 4K+ Views

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 More

Display years in different formats with C# DateTime

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 223 Views

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 More

Ulong type in C#

George John
George John
Updated on 17-Mar-2026 937 Views

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 More

ContainsKey in C#

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 2K+ Views

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 More

Convert string to bool in C#

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 2K+ Views

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 More

C# program to convert string to long

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 15K+ Views

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

KeyValuePair in C#

George John
George John
Updated on 17-Mar-2026 12K+ Views

The KeyValuePair struct in C# stores a pair of values in a single object, where one value acts as the key and the other as the value. It is commonly used in collections like List and as the element type in dictionaries. KeyValuePair is particularly useful when you need to associate two related pieces of data together without creating a custom class or when working with dictionary enumerations. Syntax Following is the syntax for creating a KeyValuePair − KeyValuePair pair = new KeyValuePair(key, value); Following is the syntax for accessing key and value ...

Read More

Sort KeyValuePairs in C#

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 973 Views

The KeyValuePair structure in C# represents a key-value pair that can be stored in collections. When working with lists of KeyValuePairs, you often need to sort them based on either the key or value. C# provides several approaches to accomplish this sorting. Syntax Following is the syntax for sorting KeyValuePairs using the Sort() method with a lambda expression − myList.Sort((x, y) => x.Value.CompareTo(y.Value)); // Sort by Value (ascending) myList.Sort((x, y) => y.Value.CompareTo(x.Value)); // Sort by Value (descending) myList.Sort((x, y) => x.Key.CompareTo(y.Key)); // Sort by Key (ascending) Following ...

Read More
Showing 1361–1370 of 1,951 articles
« Prev 1 135 136 137 138 139 196 Next »
Advertisements