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
Csharp Articles
Page 35 of 196
Get the number of elements actually contained in the ArrayList in C#
The Count property in C# is used to get the number of elements actually contained in an ArrayList. This property returns an integer representing the current number of elements, which is different from the Capacity property that indicates the total storage space available. Syntax Following is the syntax for using the Count property − int elementCount = arrayListName.Count; Count vs Capacity Count Property Capacity Property Returns the actual number of elements stored Returns the total storage space available Changes when elements are added or ...
Read MoreGet an enumerator that iterates through Collection in C#
To get an enumerator that iterates through a Collection in C#, you use the GetEnumerator() method. An enumerator provides a way to iterate through collection elements one by one using MoveNext() and Current properties. The Collection class is part of the System.Collections.ObjectModel namespace and provides a generic collection that can be customized by inheritance. Syntax Following is the syntax for getting and using an enumerator − var enumerator = collection.GetEnumerator(); while (enumerator.MoveNext()) { // Access current element using enumerator.Current } How It Works GetEnumerator() returns an IEnumerator ...
Read MoreGet the number of elements contained in SortedList in C#
The Count property in C# is used to get the number of elements contained in a SortedList. This property returns an integer representing the total count of key-value pairs stored in the collection. Syntax Following is the syntax for using the Count property − int count = sortedList.Count; Parameters The Count property does not take any parameters. It is a read-only property that returns the current number of elements. Return Value The Count property returns an int value representing the number of key-value pairs in the SortedList. Using Count Property ...
Read MoreConvert Queue To array in C#
Converting a queue to an array in C# is accomplished using the ToArray() method, which is available for the Queue generic collection. This method creates a new array containing all elements from the queue in the same order they were added (FIFO - First In, First Out). Syntax Following is the syntax for converting a queue to an array − T[] array = queue.ToArray(); Where T is the type of elements in the queue, and queue is the Queue instance. Return Value The ToArray() method returns a new array of type T[] ...
Read MoreConvert Stack to array in C#
Converting a Stack to an array in C# is accomplished using the ToArray() method. This method creates a new array containing all elements from the stack in the same order they would be popped (LIFO - Last In, First Out). The Stack class in C# is part of the System.Collections.Generic namespace and represents a collection that follows the Last In, First Out principle. Syntax Following is the syntax for converting a Stack to an array − Stack stack = new Stack(); T[] array = stack.ToArray(); How It Works The ToArray() method creates ...
Read MoreGet the number of elements contained in the Stack in C#
The Count property in C# provides an efficient way to get the number of elements contained in a Stack. This property returns an integer value representing the current number of items stored in the stack. Syntax Following is the syntax for using the Count property − Stack stack = new Stack(); int count = stack.Count; Return Value The Count property returns an int value representing the number of elements in the stack. For an empty stack, it returns 0. Stack Count Property ...
Read MoreGet the number of elements in the SortedSet in C#
The Count property in C# returns the number of elements currently stored in a SortedSet. This property provides an efficient way to determine the size of the collection without having to iterate through all elements. Syntax Following is the syntax for getting the count of elements in a SortedSet − int elementCount = sortedSet.Count; Return Value The Count property returns an int value representing the total number of elements in the SortedSet. For an empty SortedSet, it returns 0. Using Count Property with String SortedSet The following example demonstrates how to ...
Read MoreHow to get Fifth Element of the Tuple in C#?
In C#, you can access the fifth element of a tuple using the Item5 property. This property is available for tuples that contain at least five elements. The Item5 property returns the value stored at the fifth position in the tuple. Syntax Following is the syntax to access the fifth element of a tuple − var fifthElement = tupleVariable.Item5; Where tupleVariable is a tuple with at least five elements, and Item5 retrieves the value at the fifth position. Using Item5 Property Example 1 The following example demonstrates how to access the ...
Read MoreRemove the element with the specified key from the Hashtable in C#
The Hashtable class in C# provides the Remove() method to delete elements with a specified key. This method removes the key-value pair from the hashtable and reduces the count by one. Syntax Following is the syntax for the Remove() method − public virtual void Remove(object key) Parameters key − The key of the element to remove from the Hashtable. Using Remove() to Delete Single Element The following example demonstrates removing a single element from a Hashtable − using System; using System.Collections; public class Demo { ...
Read MoreCheck if two List objects are equal in C#
In C#, there are several ways to check if two List objects are equal. The default Equals() method checks for reference equality, not content equality. For comparing list contents, you need different approaches depending on your specific requirements. Using Equals() for Reference Equality The Equals() method on List objects checks if both variables reference the same object in memory − using System; using System.Collections.Generic; public class Demo { public static void Main(string[] args) { List list1 = new List { "One", "Two", "Three" }; ...
Read More