Csharp Articles

Page 42 of 196

How to find the MaxCapacity of a StringBuilder in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 312 Views

The MaxCapacity property of a StringBuilder in C# returns the maximum number of characters that the StringBuilder can hold. This is a read-only property that represents the theoretical upper limit for the StringBuilder's capacity. In most cases, the MaxCapacity is set to Int32.MaxValue (2, 147, 483, 647), which is the maximum value for a 32-bit signed integer. However, you can specify a custom maximum capacity when creating a StringBuilder using specific constructor overloads. Syntax Following is the syntax to access the MaxCapacity property − int maxCapacity = stringBuilder.MaxCapacity; Following is the syntax to ...

Read More

How to create an OrderedDictionary in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 161 Views

An OrderedDictionary in C# is a specialized collection that maintains the insertion order of key-value pairs. Unlike a regular Dictionary, it preserves the sequence in which elements were added, making it useful when order matters. The OrderedDictionary class is part of the System.Collections.Specialized namespace and provides both indexed access and key-based access to elements. Syntax Following is the syntax for creating an OrderedDictionary − OrderedDictionary dict = new OrderedDictionary(); dict.Add(key, value); dict[index] = value; // Access by index dict[key] = value; // Access by key Creating and Populating an ...

Read More

Total number of elements present in an array in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 260 Views

In C#, you can get the total number of elements present in an array using several different approaches. The most common methods are using the Length property, GetLength() method, or LongLength property for very large arrays. Syntax Following are the different ways to get array length − // Using Length property (most common) int count = arrayName.Length; // Using GetLength() method for specific dimension int count = arrayName.GetLength(0); // Using LongLength for very large arrays long count = arrayName.LongLength; Using Length Property The Length property is the most commonly used approach ...

Read More

How to find the Capacity of a StringBuilder in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 176 Views

The StringBuilder class in C# provides a Capacity property that returns the current capacity of the StringBuilder instance. The capacity represents the total number of characters that the StringBuilder can hold before it needs to allocate more memory. Syntax Following is the syntax to access the Capacity property − int capacity = stringBuilder.Capacity; How StringBuilder Capacity Works StringBuilder automatically manages its internal buffer size. When you create a StringBuilder, it starts with a default capacity of 16 characters. If you exceed this capacity, StringBuilder automatically doubles the buffer size to accommodate more characters. ...

Read More

StringCollection Class in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 862 Views

The StringCollection class in C# is a specialized collection that stores strings. It is part of the System.Collections.Specialized namespace and provides methods specifically designed for string operations, making it more efficient than generic collections when working exclusively with strings. Properties Following are the key properties of the StringCollection class − Property Description Count Gets the number of strings contained in the StringCollection. IsReadOnly Gets a value indicating whether the StringCollection is read-only. IsSynchronized Gets a value indicating whether access to the StringCollection is synchronized (thread safe). ...

Read More

Check whether the Dictionary has the specified key or not in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 212 Views

To check whether a Dictionary contains a specified key, C# provides the ContainsKey() method. This method returns true if the key exists in the dictionary, and false otherwise. Syntax Following is the syntax for the ContainsKey() method − public bool ContainsKey(TKey key) Parameters key − The key to locate in the dictionary. Return Value Returns true if the dictionary contains an element with the specified key; otherwise, false. Using ContainsKey() - Key Found The following example demonstrates checking for a key that exists in the ...

Read More

Get the handle for the Type of a specified object C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 294 Views

To get the handle for the Type of a specified object in C#, you use the Type.GetTypeHandle() method. This method returns a RuntimeTypeHandle structure that represents the type handle, which is a unique identifier for the type in the runtime environment. A type handle is useful for low-level operations and provides a lightweight way to reference types without keeping the full Type object in memory. Syntax Following is the syntax for getting a type handle − RuntimeTypeHandle handle = Type.GetTypeHandle(objectInstance); To convert back from handle to Type − Type type = ...

Read More

Get the fields of the current Type in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 359 Views

To get the fields of the current Type in C#, you can use the GetFields() method from the System.Reflection namespace. This method allows you to retrieve field information using various BindingFlags to specify which types of fields to include. Reflection in C# provides the ability to inspect and manipulate types, methods, properties, and fields at runtime. The Type.GetFields() method is particularly useful for examining the internal structure of classes. Syntax Following is the basic syntax for getting fields using reflection − Type type = typeof(ClassName); FieldInfo[] fields = type.GetFields(); To get specific types ...

Read More

Add element to HashSet in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 374 Views

The HashSet in C# is a collection that stores unique elements without duplicates. To add elements to a HashSet, you use the Add() method, which returns a boolean value indicating whether the element was successfully added. Syntax Following is the syntax for adding an element to a HashSet − bool result = hashSet.Add(element); Return Value The Add() returns − true if the element was added successfully false if the element already exists in the HashSet Using Add() Method with String HashSet Here's how to ...

Read More

Add the specified key and value into the ListDictionary in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 199 Views

The ListDictionary class in C# provides a simple dictionary implementation using a singly linked list. It is optimized for small collections (typically fewer than 10 items) and belongs to the System.Collections.Specialized namespace. To add key-value pairs, use the Add() method. Syntax Following is the syntax for adding key-value pairs to a ListDictionary − ListDictionary.Add(object key, object value); Parameters key − The key to add to the ListDictionary. Cannot be null. value − The value associated with the key. Can be null. Using Add() Method with String Values The following ...

Read More
Showing 411–420 of 1,951 articles
« Prev 1 40 41 42 43 44 196 Next »
Advertisements