Generate current month in C#

George John
Updated on 17-Mar-2026 07:04:35

11K+ Views

To generate the current month in C#, you can use the DateTime.Now property to get the current date and time, then extract the month information using various properties and formatting methods. Syntax Following is the syntax to get the current date − DateTime dt = DateTime.Now; Following is the syntax to get the month as a number − int month = dt.Month; Following is the syntax to get the month name using formatting − string monthName = dt.ToString("MMMM"); Getting Current Month as Number The Month ... Read More

Type.GetInterface() Method in C#

AmitDiwan
Updated on 17-Mar-2026 07:04:35

290 Views

The Type.GetInterface() method in C# is used to get a specific interface implemented or inherited by the current Type. This method searches through all interfaces implemented by a type and returns the one that matches the specified name. Syntax Following are the overloads of the GetInterface() method − public Type GetInterface(string name); public abstract Type GetInterface(string name, bool ignoreCase); Parameters name − The name of the interface to find. ignoreCase − A Boolean value indicating whether to perform a case-insensitive search for the interface name. Return ... Read More

What are the differences between a static and a non-static class in C#?

Arjun Thakur
Updated on 17-Mar-2026 07:04:35

2K+ Views

A static class in C# cannot be instantiated and contains only static members, while a non-static class can be instantiated to create objects and can contain both static and instance members. The key difference is that static classes are designed for utility functions that don't require object state, whereas non-static classes represent entities that can have multiple instances with their own data. Syntax Following is the syntax for declaring a static class − public static class ClassName { public static void StaticMethod() { ... Read More

Clear a StringBuilder in C#

karthikeya Boyini
Updated on 17-Mar-2026 07:04:35

2K+ Views

The Clear() method in C# is used to remove all characters from a StringBuilder object, effectively resetting it to an empty state. This method is efficient as it doesn't create a new object but simply resets the internal character buffer. Syntax Following is the syntax for the Clear() method − stringBuilder.Clear(); Return Value The Clear() method returns a reference to the same StringBuilder instance with all characters removed. This allows for method chaining. Using StringBuilder Clear() Method Example The following example demonstrates how to clear a StringBuilder and check its ... Read More

How to initialize an empty DateTime in C#

Samual Sam
Updated on 17-Mar-2026 07:04:35

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

UInt16.ToString Method in C# with Examples

AmitDiwan
Updated on 17-Mar-2026 07:04:35

201 Views

The UInt16.ToString() method in C# is used to convert the numeric value of the current UInt16 instance to its equivalent string representation. The UInt16 data type represents a 16-bit unsigned integer with a range from 0 to 65, 535. This method has multiple overloads that allow you to format the string representation using different format specifiers, culture-specific formatting, and format providers. Syntax Following is the syntax for the basic ToString() method − public override string ToString() Following is the syntax for ToString() with format specifier − public string ToString(string format) ... Read More

How to find the first character of a string in C#?

George John
Updated on 17-Mar-2026 07:04:35

45K+ Views

To get the first character of a string in C#, you can use several approaches. The most common methods are using string indexing, the Substring() method, or the First() LINQ method. Syntax Following are the different syntaxes to get the first character − char firstChar = str[0]; string firstChar = str.Substring(0, 1); char firstChar = str.First(); Using String Indexing The simplest way to get the first character is using string indexing with [0]. This returns a char type − using System; public class Demo ... Read More

Access a character in C# StringBuilder

Samual Sam
Updated on 17-Mar-2026 07:04:35

491 Views

A StringBuilder in C# allows you to access individual characters using the indexer syntax, similar to accessing elements in an array. The indexer provides both read and write access to characters at specific positions within the StringBuilder. Syntax Following is the syntax for accessing a character in StringBuilder − char character = stringBuilder[index]; Following is the syntax for modifying a character in StringBuilder − stringBuilder[index] = 'newCharacter'; Parameters index − The zero-based position of the character to access or modify. Return Value Returns the character ... Read More

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

karthikeya Boyini
Updated on 17-Mar-2026 07:04:35

486 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

What is the Item property of Hashtable class in C#?

Arjun Thakur
Updated on 17-Mar-2026 07:04:35

175 Views

The Item property of the Hashtable class in C# gets or sets the value associated with the specified key. This property uses the indexer syntax [key] and allows you to both retrieve existing values and add new key-value pairs to the hashtable. When a key does not exist in the hashtable, you can use the Item property to add it along with its value. This makes it convenient for both accessing and modifying hashtable contents. Syntax Following is the syntax for using the Item property − // Getting a value object value = hashtable[key]; ... Read More

Advertisements