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 Nizamuddin Siddiqui
Page 5 of 196
How to fetch a property value dynamically in C#?
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 MoreHow to delete all files and folders from a path in C#?
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 MoreWhat is the main difference between int.Parse() and Convert.ToInt32 in C#?
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 MoreHow can we update the values of a collection using LINQ in C#?
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 MoreHow to replace line breaks in a string in C#?
In C#, you often need to remove or replace line breaks from strings when processing text data. Line breaks can appear as (line feed), \r (carriage return), or \r (Windows-style line endings). This article demonstrates different approaches to handle line breaks in strings. Common Line Break Characters Different operating systems use different line break characters − − Line Feed (LF), used on Unix/Linux/Mac \r − Carriage Return (CR), used on older Mac systems \r − Carriage Return + Line Feed (CRLF), used on Windows Line Break Types ...
Read MoreHow to find items in one list that are not in another list in C#?
Finding items in one list that are not present in another list is a common task in C# programming. LINQ provides multiple approaches to solve this problem, with the Except() method being the most straightforward for simple data types and the Where() clause being more flexible for complex objects. Syntax Using the Except() method for simple types − var result = list1.Except(list2); Using Where() clause with All() for complex objects − var result = list1.Where(item1 => list2.All(item2 => condition)); Using Except() Method The Except() method is a LINQ set ...
Read MoreHow can we return null from a generic method in C#?
Generics in C# allow us to define classes and methods with placeholders for types, which are replaced with specific types at compile time. However, returning null from a generic method requires special handling because the compiler cannot determine whether the generic type T is a reference type or value type. When you try to return null directly from a generic method, you'll encounter a compilation error because null cannot be assigned to value types like int, double, etc. The Problem Here's what happens when we try to return null directly from a generic method − ...
Read MoreWhat is the use of yield return in C#?
The yield return keyword in C# enables lazy evaluation and custom iteration over collections. When you use yield return, the method becomes an iterator that returns values one at a time, pausing execution between each return and resuming where it left off when the next value is requested. Syntax Following is the syntax for using yield return − public static IEnumerable MethodName() { // some logic yield return value; // more logic yield return anotherValue; } You can ...
Read MoreWhat is the difference between Last() and LastOrDefault() in Linq C#?
Both Last() and LastOrDefault() are LINQ extension methods that retrieve the last element from a sequence. The key difference is how they handle empty sequences or no matching elements: Last() throws an InvalidOperationException when no element is found, while LastOrDefault() returns the default value for the type (null for reference types, 0 for integers, etc.). Syntax Following is the syntax for Last() method − public static T Last(this IEnumerable source); public static T Last(this IEnumerable source, Func predicate); Following is the syntax for LastOrDefault() method − public static T LastOrDefault(this IEnumerable source); ...
Read MoreHow to sort 0,1,2 in an Array (Dutch National Flag) without extra space using C#?
The Dutch National Flag problem is a classic algorithm challenge that sorts an array containing only 0s, 1s, and 2s in a single pass without using extra space. This problem was originally described by Dutch computer scientist Edsger Dijkstra and is also known as the Three-Way Partitioning algorithm. The algorithm uses three pointers to partition the array into three regions: all 0s on the left, all 1s in the middle, and all 2s on the right. Algorithm Overview The algorithm maintains three pointers − low − Points to the boundary of the 0s region mid ...
Read More