Csharp Articles

Page 150 of 196

How to initialize an empty DateTime in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 3K+ Views

In C#, there are several ways to initialize an empty or default DateTime value. The most common approach is using DateTime.MinValue, which represents the minimum possible date and time value in the .NET framework. Syntax Following are the different ways to initialize an empty DateTime − DateTime dt1 = DateTime.MinValue; DateTime dt2 = new DateTime(); DateTime dt3 = default(DateTime); Using DateTime.MinValue The DateTime.MinValue property returns the minimum possible DateTime value, which is January 1, 0001 at 12:00:00 AM − using System; public class Demo { public static ...

Read More

C# General Date Short Time ("g") Format Specifier

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 481 Views

The General Date Short Time ("g") format specifier in C# is a standard DateTime format that combines the short date pattern ("d") and short time pattern ("t"), separated by a space. This format provides a compact representation of both date and time information. Syntax Following is the syntax for using the "g" format specifier − dateTime.ToString("g") dateTime.ToString("g", CultureInfo) The format pattern varies based on the current culture or specified culture. It typically displays − Short date − MM/dd/yyyy or dd/MM/yyyy depending on culture Short time − HH:mm or h:mm tt (12-hour with ...

Read More

C# DateTime Max Value

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

The DateTime.MaxValue property in C# returns the maximum possible value for a DateTime object. This represents the largest date and time that can be stored in a DateTime structure, which is December 31, 9999 at 11:59:59.9999999 PM. This property is useful when you need to initialize a DateTime variable with the highest possible value, compare dates to find the maximum, or set upper bounds in date range validations. Syntax Following is the syntax for accessing the maximum DateTime value − DateTime maxValue = DateTime.MaxValue; DateTime MaxValue Example The following example demonstrates how ...

Read More

C# Program to return specified number of elements from the end of an array

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

The TakeLast() method in C# returns a specified number of elements from the end of a sequence. It's part of the LINQ library and provides an efficient way to extract the last few elements from arrays, lists, or other enumerable collections. Syntax Following is the syntax for using TakeLast() − IEnumerable TakeLast(int count) Parameters count − The number of elements to return from the end of the sequence. Return Value Returns an IEnumerable containing the specified number of elements from the end of the source sequence. Using TakeLast() ...

Read More

C# TimeSpan Min value

Samual Sam
Samual Sam
Updated on 17-Mar-2026 604 Views

The TimeSpan structure in C# represents a time interval or duration. The TimeSpan.MinValue property returns the minimum possible value that a TimeSpan can represent, which is approximately -10.7 million days. This minimum value is useful when you need to initialize a TimeSpan variable to the smallest possible value or when performing comparisons to find the minimum time span in a collection. Syntax Following is the syntax to access the minimum TimeSpan value − TimeSpan.MinValue Return Value The TimeSpan.MinValue property returns a TimeSpan object representing the minimum possible time interval, which equals approximately ...

Read More

C# TimeSpan Max value

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

The TimeSpan structure in C# represents a time interval or duration. The TimeSpan.MaxValue property returns the maximum possible value that a TimeSpan can represent. Syntax Following is the syntax to get the maximum TimeSpan value − TimeSpan.MaxValue Return Value The MaxValue property returns a TimeSpan object representing the maximum time span value, which is approximately 10.7 million days or about 29, 227 years. TimeSpan.MaxValue Breakdown 10675199.02:48:05.4775807 Days: 10, 675, 199 Hours: 02 ...

Read More

C# Program to get the absolute value of the time

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 1K+ Views

To get the absolute value of time, use the TimeSpan.Duration()

Read More

C# Console BufferHeight Property

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

The Console.BufferHeight property in C# gets or sets the height of the buffer area in rows. The buffer height represents the maximum number of text rows that can be stored in the console's internal buffer before older lines are discarded. This property is particularly useful when you need to control how much output history the console retains or when working with console applications that produce large amounts of output. Syntax Following is the syntax for using the BufferHeight property − // Get buffer height int height = Console.BufferHeight; // Set buffer height Console.BufferHeight = ...

Read More

Display the default if element is not found in a C# List

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

When working with C# Lists, attempting to access an element that doesn't exist can throw exceptions. To safely handle empty lists or missing elements, you can use methods like FirstOrDefault() which return the default value for the type instead of throwing an exception. For reference types like strings, the default value is null. For value types like int, float, or double, the default value is 0. For bool, it's false. Syntax Following is the syntax for using FirstOrDefault() on a List − list.FirstOrDefault(); list.FirstOrDefault(condition); You can also specify a custom default value using ...

Read More

Difference between TimeSpan Seconds() and TotalSeconds()

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 4K+ Views

The TimeSpan.Seconds property returns only the seconds component of a time span, whereas TimeSpan.TotalSeconds property converts the entire time duration into seconds. Understanding this difference is crucial when working with time calculations in C#. Syntax Following is the syntax for accessing the seconds component − TimeSpan ts = new TimeSpan(days, hours, minutes, seconds, milliseconds); int seconds = ts.Seconds; Following is the syntax for getting total seconds − TimeSpan ts = new TimeSpan(days, hours, minutes, seconds, milliseconds); double totalSeconds = ts.TotalSeconds; How It Works TimeSpan: 1 ...

Read More
Showing 1491–1500 of 1,951 articles
« Prev 1 148 149 150 151 152 196 Next »
Advertisements