We can use Reflection to fetch a property value dynamically in C#. Reflection provides objects of type Type that describe assemblies, modules, and types, allowing us to dynamically access and manipulate object properties at runtime without knowing them at compile time. The System.Reflection namespace and System.Type class work together to enable dynamic property access through methods like GetProperty() and GetValue(). Syntax Following is the syntax for getting a property value dynamically − Type type = typeof(ClassName); PropertyInfo property = type.GetProperty("PropertyName"); object value = property.GetValue(instanceObject, null); Following is the syntax for setting a property ... Read More
Reversing a number is a fundamental programming problem where we reverse the digits of an integer. For example, reversing 12345 gives 54321. This article explores different approaches to reverse a number in C#. Problem Description Given an integer, we need to reverse its digits and return the reversed number. The process involves extracting digits from right to left and reconstructing them from left to right. Examples Input: 12345 Output: 54321 Explanation: The digits are reversed from their original order. Input: 8299 Output: 9928 Explanation: The digits 8, 2, 9, 9 become 9, 9, 2, 8. ... Read More
The Cartesian coordinate system is divided into four quadrants based on the signs of x and y coordinates. In this article, we will learn how to determine which quadrant a given point lies in using C#. Understanding Quadrants The coordinate plane is divided into four regions called quadrants − Quadrant I: x > 0 and y > 0 (both positive) Quadrant II: x < 0 and y > 0 (x negative, y positive) Quadrant III: x < 0 and y < 0 (both negative) Quadrant IV: x > 0 and y < 0 (x positive, y ... Read More
Deleting all files and folders from a directory is a common task in C# file operations. The System.IO namespace provides the DirectoryInfo class and Directory class that offer multiple approaches to accomplish this task safely and efficiently. Syntax Following is the syntax for using DirectoryInfo to delete directories and files − DirectoryInfo di = new DirectoryInfo(path); di.Delete(true); // true = recursive delete Following is the syntax for using Directory class methods − Directory.Delete(path, true); // true = recursive delete Using DirectoryInfo for Recursive Deletion The DirectoryInfo class provides detailed ... Read More
The OrderedDictionary class in C# provides the Remove() method to remove entries with a specified key. The OrderedDictionary maintains the insertion order of elements while allowing both key-based and index-based access. Syntax Following is the syntax for removing an entry by key from an OrderedDictionary − orderedDictionary.Remove(key); Parameters key − The key of the entry to remove from the OrderedDictionary. Return Value The Remove() method does not return a value. It removes the entry if the key exists, or does nothing if the key is not found. ... Read More
The shallow copy of a SortedList object in C# creates a new SortedList instance with the same key-value pairs as the original, but both collections share references to the same objects. This is accomplished using the Clone() method. Syntax Following is the syntax for creating a shallow copy of SortedList − SortedList clonedList = (SortedList)originalList.Clone(); What is a Shallow Copy? A shallow copy creates a new collection object, but the elements inside both the original and copied collections point to the same memory locations. For value types like strings and integers, this behaves ... Read More
Both int.Parse() and Convert.ToInt32() methods in C# are used to convert string representations of numbers to integers. However, they handle null values and certain edge cases differently. The key difference is that Convert.ToInt32() handles null values gracefully by returning 0, while int.Parse() throws an ArgumentNullException when encountering null. Syntax Following is the syntax for int.Parse() method − int result = int.Parse(stringValue); Following is the syntax for Convert.ToInt32() method − int result = Convert.ToInt32(stringValue); Using int.Parse() with Valid String The int.Parse() method converts a valid numeric string to an ... Read More
The StringDictionary class in C# provides the Remove() method to delete entries with a specified key. This method removes both the key and its associated value from the collection. StringDictionary is case-insensitive, meaning keys are automatically converted to lowercase. Syntax Following is the syntax for the Remove() method − public virtual void Remove(string key) Parameters key − The string key to remove from the StringDictionary. Using Remove() Method Example using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main() ... Read More
To get the hash code for a specified key in a Hashtable, you need to use the GetHash() method. This method is protected in the base Hashtable class, so you must create a derived class to access it. The hash code is an integer value used internally by the hashtable to determine where to store the key-value pair. Syntax Following is the syntax to access the protected GetHash() method − public class CustomHashtable : Hashtable { public int GetHashCodeForKey(object key) { return GetHash(key); } ... Read More
LINQ provides several approaches to update collection values in C#. While LINQ is primarily designed for querying data, you can combine it with other methods to modify collection elements efficiently. Using ForEach with List Collections The List class provides a ForEach method that can be used to update all elements in the collection − using System; using System.Collections.Generic; namespace DemoApplication { class Program { static void Main(string[] args) { List fruits ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance