Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 719 of 2109
How to find the MaxCapacity of a StringBuilder in C#?
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 MoreHow to create an OrderedDictionary in C#?
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 MoreTotal number of elements present in an array in C#
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 MoreHow to find the Capacity of a StringBuilder in C#
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 MoreStringCollection Class in C#
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 MoreCheck whether the Dictionary has the specified key or not in C#
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 MoreGet the handle for the Type of a specified object C#
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 MoreGet the fields of the current Type in C#
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 MoreAdd element to HashSet in C#
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 MoreAdd the specified key and value into the ListDictionary in C#
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