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 123 of 196
C# program to list the difference between two lists
To find the difference between two lists in C#, you can use the Except() method from LINQ. This method returns elements from the first list that are not present in the second list, effectively giving you the set difference. Syntax Following is the syntax for using Except() method to find list difference − IEnumerable result = list1.Except(list2); Where list1 is the source list and list2 contains elements to exclude from the result. Using Except() Method The Except() method performs a set difference operation, returning elements that exist in the first collection but ...
Read MoreC# program to merge two sorted arrays into one
Merging two sorted arrays in C# involves combining the elements of both arrays into a single array while maintaining the sorted order. This is a fundamental operation used in algorithms like merge sort and when combining datasets. Approaches to Merge Sorted Arrays There are two main approaches to merge sorted arrays − Simple Concatenation: Append all elements from both arrays and then sort the result. Merge Algorithm: Compare elements from both arrays and merge them in sorted order without additional sorting. Using Simple Concatenation The simplest approach is to ...
Read MoreWhat is unmanaged code in C#?
Unmanaged code in C# refers to code that executes outside the control of the .NET Common Language Runtime (CLR). This type of code is also known as unsafe code because it bypasses many of the safety mechanisms that make C# a managed language. In C#, unmanaged code typically involves the use of pointers and direct memory manipulation, similar to languages like C and C++. To use unmanaged code, you must explicitly mark code blocks with the unsafe keyword. Key Characteristics of Unmanaged Code Applications that are not under the control of the CLR are unmanaged. ...
Read MoreFlow control in try catch finally in C#
The flow control in try-catch-finally blocks in C# follows a specific execution order depending on whether exceptions occur. Understanding this flow is crucial for proper error handling and resource management in your applications. Syntax Following is the syntax for try-catch-finally blocks − try { // Code that may throw an exception } catch (ExceptionType e) { // Exception handling code } finally { // Code that always executes } Flow Control Rules The try block executes first and contains code that might ...
Read MoreHow to convert Decimal to Binary using C#?
Converting decimal numbers to binary in C# can be accomplished using the division and modulus operators. This process involves repeatedly dividing the decimal number by 2 and collecting the remainders, which form the binary representation when reversed. The algorithm works by dividing the decimal value by 2, storing the remainder (which is always 0 or 1), and continuing until the quotient becomes 0. The binary representation is formed by reading the remainders in reverse order. Algorithm The conversion process follows these steps − Divide the decimal number by 2 Store the remainder ...
Read MoreHow to check if String is Palindrome using C#?
A palindrome is a string that reads the same forwards and backwards, such as "Level", "madam", or "racecar". In C#, there are several ways to check if a string is a palindrome. Using Array.Reverse() Method The simplest approach is to reverse the string and compare it with the original. First, convert the string to a character array − char[] ch = str.ToCharArray(); Then reverse the array using Array.Reverse() − Array.Reverse(ch); Finally, compare the reversed string with the original using case-insensitive comparison − bool res = str.Equals(rev, StringComparison.OrdinalIgnoreCase); ...
Read MoreLinkedList in C#
The LinkedList class in C# is a doubly-linked list implementation found in the System.Collections.Generic namespace. Unlike arrays or lists, a linked list allows for efficient insertion and removal of elements at any position without the need to shift other elements. Each element in a LinkedList is stored in a node that contains the data and references to both the next and previous nodes. This structure enables fast insertion and deletion operations at the cost of direct indexed access. Syntax Following is the syntax for creating a LinkedList − LinkedList linkedList = new LinkedList(); ...
Read MoreHow to find the file using C#?
Use the Directory.GetFiles and Directory.GetDirectories methods in C# to search for files in a directory structure. These methods provide powerful options to recursively search through subdirectories and apply search patterns. Syntax Following is the syntax for finding files using Directory.GetFiles − string[] files = Directory.GetFiles(path, searchPattern, SearchOption.AllDirectories); Following is the syntax for finding directories using Directory.GetDirectories − string[] directories = Directory.GetDirectories(path, searchPattern, SearchOption.AllDirectories); Parameters path − The directory path to search in searchPattern − The search string to match file/folder names (supports wildcards like * and ?) SearchOption ...
Read MoreC# program to merge two Dictionaries
Merging dictionaries in C# involves combining two or more Dictionary objects into one. This can be useful when you need to consolidate data from different sources or combine configuration settings. There are several approaches to merge dictionaries, each with different behaviors for handling duplicate keys. Syntax Following is the syntax for creating dictionaries − Dictionary dict = new Dictionary(); Following is the syntax for adding elements − dict.Add(key, value); dict[key] = value; // overwrites if key exists Using HashSet to Merge Keys Only This approach merges only the ...
Read MoreSemaphore in C#
A Semaphore in C# is a synchronization primitive that controls access to a pool of resources by allowing a specified number of threads to enter a critical section simultaneously. It maintains a count of available permits and blocks threads when the limit is reached. The System.Threading.Semaphore class provides all the methods and properties needed to implement semaphore functionality in multithreaded applications. Syntax The basic syntax for creating a semaphore − Semaphore semaphore = new Semaphore(initialCount, maximumCount); To acquire and release the semaphore − semaphore.WaitOne(); // Acquire the semaphore ...
Read More