Programming Articles

Page 6 of 2547

Get the fields of the current Type in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 352 Views

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 More

C# Program to check if a path is a directory or a file

Ankit kumar
Ankit kumar
Updated on 17-Mar-2026 5K+ Views

In C# programming, determining whether a given path points to a directory or a file is a common task. A directory (also called a folder) is a container that organizes files and other directories on your computer. A file is a collection of data stored with a unique name and path. The .NET framework provides built-in methods in the System.IO namespace to check path types efficiently using File.Exists() and Directory.Exists() methods. Syntax Following is the syntax for checking file existence − public static bool File.Exists(string path); Following is the syntax for checking directory ...

Read More

Check if two ArrayList objects are equal in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 406 Views

Checking if two ArrayList objects are equal in C# involves understanding how the Equals() method works. The ArrayList.Equals() method checks for reference equality, not content equality. This means it only returns true if both variables point to the same object in memory. Understanding ArrayList.Equals() Behavior The Equals()

Read More

Add element to HashSet in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 361 Views

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 More

How to find the unique triplet that is close to the given target using C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 285 Views

The Three Sum Closest problem asks us to find a triplet of numbers in an array whose sum is closest to a given target value. This problem uses the Two Pointers pattern and is similar to the Triplet Sum to Zero problem. We iterate through the array, taking one number at a time as the first element of our triplet. For each fixed element, we use two pointers to find the best pair that makes the triplet sum closest to the target. At every step, we save the difference between the triplet sum and the target, comparing it with ...

Read More

Check if a SortedSet is a subset of the specified collection in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 150 Views

The SortedSet class in C# provides the IsSubsetOf() method to check if all elements of one SortedSet are contained within another collection. A subset means that every element in the first set exists in the second set, though the second set may contain additional elements. Syntax Following is the syntax for the IsSubsetOf() method − public bool IsSubsetOf(IEnumerable other) Parameters other − The collection to compare with the current SortedSet. Return Value Returns true if the current SortedSet is a subset of the specified collection; otherwise, false. ...

Read More

How to find all the unique quadruplets that is close to zero using C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 304 Views

Finding all unique quadruplets that sum to zero in C# is a classic algorithmic problem known as the 4Sum problem. The goal is to find all combinations of four distinct elements from an array whose sum equals zero, avoiding duplicate quadruplets. Problem Approaches There are multiple approaches to solve this problem, each with different time and space complexities − Approach Time Complexity Space Complexity Description Brute Force O(n4) O(1) Four nested loops checking all combinations HashSet Optimization O(n3) O(n) Use HashSet to find fourth element Two ...

Read More

Check if two HashSet objects are equal in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 436 Views

To check if two HashSet objects are equal in C#, you can use the Equals() method or the SetEquals() method. However, these methods work differently − Equals() checks for reference equality, while SetEquals() checks for content equality regardless of order. Syntax Following is the syntax for checking HashSet equality using Equals() − bool result = hashSet1.Equals(hashSet2); Following is the syntax for checking HashSet content equality using SetEquals() − bool result = hashSet1.SetEquals(hashSet2); Using Equals() for Reference Equality The Equals() method checks if two HashSet objects reference the same instance ...

Read More

Add the specified key and value into the ListDictionary in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 191 Views

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

C# Program to Copy a File

Ankit kumar
Ankit kumar
Updated on 17-Mar-2026 544 Views

C# provides built-in functionality for file operations through the System.IO namespace. File copying is a fundamental operation that allows you to duplicate existing files to new locations or with different names. The File.Copy() method offers a straightforward way to copy files with optional overwrite capabilities. Syntax The File.Copy() method has two overloads − public static void Copy(string sourceFileName, string destFileName); public static void Copy(string sourceFileName, string destFileName, bool overwrite); Parameters sourceFileName − The path and name of the file to copy. destFileName − The path and name of the ...

Read More
Showing 51–60 of 25,467 articles
« Prev 1 4 5 6 7 8 2547 Next »
Advertisements