Chandu yadav

Chandu yadav

810 Articles Published

Articles by Chandu yadav

Page 8 of 81

TimeSpan.From methods in C# ()

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

The TimeSpan.From methods in C# provide a convenient way to create TimeSpan objects from specific time units. These static methods include FromDays, FromHours, FromMinutes, FromSeconds, FromMilliseconds, and FromTicks. Each method takes a numeric value and returns a TimeSpan representing that duration in the specified unit. This approach is more readable than manually calculating ticks or using TimeSpan constructors. Syntax Following are the syntax formats for the most commonly used TimeSpan.From methods − TimeSpan.FromDays(double value) TimeSpan.FromHours(double value) TimeSpan.FromMinutes(double value) TimeSpan.FromSeconds(double value) TimeSpan.FromMilliseconds(double value) TimeSpan.FromTicks(long value) Parameters All TimeSpan.From methods (except FromTicks) accept a ...

Read More

Clear a Hashtable in C#

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

The Clear() method in C# is used to remove all key-value pairs from a Hashtable. This method provides an efficient way to empty the entire Hashtable in a single operation, resetting its count to zero while maintaining the original capacity. Syntax Following is the syntax for the Clear()

Read More

Format TimeSpan in C#

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

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 More

What are mixed arrays in C#?

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

Mixed arrays in C# refer to arrays that can store elements of different data types. They were historically used as a combination of multi-dimensional arrays and jagged arrays, but this terminology is largely obsolete in modern C#. Instead, we use object[] arrays or collections to achieve similar functionality. Note − The traditional "mixed arrays" concept became obsolete after .NET 4.0, as modern C# provides better alternatives like generic collections and tuples. Syntax Following is the syntax for creating an array that can hold mixed data types − object[] arrayName = new object[] { value1, value2, ...

Read More

How to handle empty collections in C#

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

Handling empty collections in C# is a common scenario that can lead to runtime errors if not managed properly. The DefaultIfEmpty() method from LINQ provides an elegant solution to work with empty collections by returning a default value when the collection contains no elements. When working with collections, you often need to ensure that operations don't fail on empty collections. The DefaultIfEmpty() method prevents exceptions and provides predictable behavior. Syntax Following is the syntax for using DefaultIfEmpty() method − collection.DefaultIfEmpty() collection.DefaultIfEmpty(defaultValue) Parameters defaultValue (optional) − The value to return if the ...

Read More

String format for Double in C#

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

The String.Format method in C# provides powerful options for formatting double values. You can control decimal places, add thousands separators, and apply various numeric formatting patterns to display doubles exactly as needed. Syntax Following is the basic syntax for formatting double values − String.Format("{0:format_specifier}", double_value) Common format specifiers for doubles − {0:0.000} // Fixed decimal places {0:0, 0.0} // Thousands separator with decimal {0:F2} // Fixed-point notation with 2 decimals {0:N2} ...

Read More

Display years in different formats with C# DateTime

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 216 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

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

How to sort a list in C#?

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

Sorting a list in C# can be accomplished using several methods. The List class provides the Sort() method for in-place sorting, while LINQ offers additional sorting capabilities. This article demonstrates various approaches to sort lists in ascending and descending order. Syntax Following is the syntax for the basic Sort() method − list.Sort(); Following is the syntax for sorting with a custom comparer − list.Sort((x, y) => y.CompareTo(x)); // descending order Using List.Sort() Method The Sort() method sorts the elements in the entire list using the default comparer for the ...

Read More

KeyNotFoundException in C#

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

The KeyNotFoundException in C# is thrown when you attempt to access a key that does not exist in a Dictionary collection. This exception occurs specifically when using the indexer syntax dictionary[key] to retrieve a value. When KeyNotFoundException Occurs This exception is thrown in the following scenarios − Accessing a Dictionary using dict[key] where the key doesn't exist Using the indexer on collections like SortedDictionary or SortedList with non-existent keys Attempting to retrieve values from key-value collections without checking key existence Dictionary Key Access ...

Read More
Showing 71–80 of 810 articles
« Prev 1 6 7 8 9 10 81 Next »
Advertisements