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
Articles by AmitDiwan
Page 200 of 840
C# Copy() Method
The String.Copy() method in C# creates a new string instance with the same value as the specified string. While strings are immutable in C#, this method ensures you get a completely separate string object in memory, which can be useful in certain scenarios involving string interning. Syntax Following is the syntax for the String.Copy() method − public static string Copy(string str); Parameters str − The string to copy. Cannot be null. Return Value Returns a new string with the same value as the input string but as a separate ...
Read MoreInsert an object at the top of the Stack in C#
To insert an object at the top of the Stack in C#, you use the Push() method. The Stack collection follows the LIFO (Last In, First Out) principle, meaning the most recently added element is always at the top and will be the first one removed. Syntax Following is the syntax for the Push() method − stack.Push(item); Parameters item: The object to insert at the top of the Stack. The value can be null for reference types. How It Works When you call Push(), the new element is added to the ...
Read MoreCreating a synchronized wrapper for the Hashtable in C#
A synchronized wrapper for the Hashtable in C# ensures thread-safe operations when multiple threads access the same Hashtable simultaneously. By default, Hashtable collections are not synchronized, but you can create a thread-safe version using the Hashtable.Synchronized() method. Syntax Following is the syntax for creating a synchronized Hashtable wrapper − Hashtable synchronizedHashtable = Hashtable.Synchronized(originalHashtable); To check if a Hashtable is synchronized, use the IsSynchronized property − bool isSynchronized = hashtable.IsSynchronized; Understanding Thread Safety Hashtable Thread Safety Regular Hashtable IsSynchronized ...
Read MoreCount the number of key/value pairs in the Hashtable in C#
In C#, the Hashtable class provides the Count property to determine the number of key/value pairs stored in the collection. This property returns an integer value representing the total count of elements currently in the Hashtable. Syntax Following is the syntax for accessing the Count property − int count = hashtable.Count; Parameters The Count property does not take any parameters. It is a read-only property that returns the current number of key/value pairs in the Hashtable. Return Value The Count property returns an int value representing the total number of key/value ...
Read MoreGet the members of the current Type in C#
In C#, you can use reflection to inspect the members of a type at runtime. The Type.GetMembers() method returns an array of MemberInfo objects representing all public members of a type, including fields, properties, methods, constructors, and events. Syntax Following is the syntax to get all members of a type − Type type = typeof(ClassName); MemberInfo[] members = type.GetMembers(); To filter members using binding flags − MemberInfo[] members = type.GetMembers(BindingFlags.Public | BindingFlags.Instance); Using GetMembers() Without Binding Flags When called without parameters, GetMembers() returns all public members including inherited ones ...
Read MoreCreating an empty case-sensitive HybridDictionary Class in C#
The HybridDictionary class in C# is a collection that combines the performance benefits of both ListDictionary and Hashtable. It automatically switches between these implementations based on the number of elements. For small collections, it uses ListDictionary, and for larger collections, it switches to Hashtable for better performance. When creating a HybridDictionary, you can specify whether it should be case-sensitive or case-insensitive through its constructor parameter. Syntax Following is the syntax for creating a case-sensitive HybridDictionary − HybridDictionary dict = new HybridDictionary(false); Following is the syntax for creating a case-insensitive HybridDictionary − ...
Read MoreCreating an empty HybridDictionary with specified case sensitivity in C#
The HybridDictionary class in C# is a collection that combines the benefits of both ListDictionary and Hashtable. It automatically switches from a list-based implementation to a hash table when the collection grows beyond a certain size. By default, HybridDictionary is case-sensitive, but you can specify case sensitivity behavior during initialization. Syntax Following is the syntax for creating an empty HybridDictionary with default case sensitivity − HybridDictionary dictionary = new HybridDictionary(); Following is the syntax for creating an empty HybridDictionary with specified case sensitivity − HybridDictionary dictionary = new HybridDictionary(bool caseInsensitive); ...
Read MoreGet the TypeCode for value type Int16 in C#
The GetTypeCode() method in C# returns a TypeCode enumeration that identifies the type of a value. For Int16 (short) values, this method consistently returns TypeCode.Int16 regardless of the actual numeric value stored. Syntax Following is the syntax for getting the TypeCode of an Int16 value − TypeCode typeCode = int16Variable.GetTypeCode(); Return Value The GetTypeCode() method returns TypeCode.Int16 for all short variables, which represents the 16-bit signed integer type. Using GetTypeCode() with Int16 Values Example using System; public class Demo { public static void Main() { ...
Read MoreSortedSet Class in C#
The SortedSet class in C# represents a collection of objects that is maintained in sorted order. Unlike regular sets, SortedSet automatically keeps elements sorted and ensures uniqueness − duplicate elements are not allowed. SortedSet is part of the System.Collections.Generic namespace and provides efficient operations for adding, removing, and searching elements while maintaining the sorted order. Syntax Following is the syntax for creating and using a SortedSet − SortedSet setName = new SortedSet(); setName.Add(element); Key Properties Property Description Comparer Gets the IComparer object that is used ...
Read MoreGetting the value at the specified index of a SortedList object in C#
In C#, the SortedList class provides the GetByIndex() method to retrieve the value at a specific index position. Unlike dictionary access by key, this method allows you to access values by their ordered position within the sorted collection. Syntax Following is the syntax for using GetByIndex() method − public virtual object GetByIndex(int index); Parameters index: The zero-based index of the value to retrieve from the SortedList. Return Value The method returns an object representing the value at the specified index position. If the index is out of ...
Read More