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
Server Side Programming Articles
Page 818 of 2109
C# program to find additional values in two lists
Finding additional values between two lists is a common requirement in C# programming. This involves identifying elements that exist in one list but not in another. The Except() method from LINQ provides an efficient way to find the difference between two collections. Syntax The Except() method returns elements from the first sequence that are not present in the second sequence − IEnumerable result = list1.Except(list2); To find differences in both directions, you can use − var onlyInList1 = list1.Except(list2); var onlyInList2 = list2.Except(list1); Using Except() to Find Additional Values ...
Read MoreHow to iterate two Lists or Arrays with one foreach statement in C#?
When working with multiple collections in C#, you often need to iterate through them simultaneously. The Zip() method from LINQ provides an elegant solution to combine two arrays or lists and iterate them with a single foreach statement. Syntax Following is the syntax for using the Zip() method to combine two collections − var result = collection1.Zip(collection2, (item1, item2) => new { Prop1 = item1, Prop2 = item2 }); The lambda expression defines how to combine elements from both collections into a new anonymous object. Using Zip() with Arrays The Zip() method ...
Read MoreC# program to find the length of the Longest Consecutive 1's in Binary Representation of a given integer
Finding the longest consecutive 1's in the binary representation of a number is a common bit manipulation problem. The key is to use the bitwise AND operation combined with left shift to eliminate consecutive 1's one group at a time. Syntax The core operation uses bitwise AND with left shift − i = (i & (i
Read MoreC# int.Parse Vs int.TryParse Method
The int.Parse and int.TryParse methods in C# are used to convert string representations of numbers to integers. The key difference lies in how they handle conversion failures: int.Parse throws an exception when the string cannot be converted, while int.TryParse returns a boolean value indicating success or failure. Syntax Following is the syntax for int.Parse method − int result = int.Parse(string); Following is the syntax for int.TryParse method − bool success = int.TryParse(string, out int result); Using int.Parse Method The int.Parse method directly converts a valid string to an integer. ...
Read MoreC# Program to remove duplicate characters from String
Removing duplicate characters from a string is a common programming task in C#. There are several approaches to achieve this, with HashSet being one of the most efficient methods due to its automatic handling of unique elements. Using HashSet to Remove Duplicates The HashSet collection automatically maintains unique elements, making it perfect for removing duplicate characters from a string − using System; using System.Linq; using System.Collections.Generic; class Program { static void Main(string[] args) { string myStr = "kkllmmnnoo"; Console.WriteLine("Initial String: ...
Read MoreHow to validate a string for a numeric representation using TryParse in C#
The TryParse method in C# is used to validate whether a string contains a valid numeric representation. It attempts to convert the string to a number and returns true if successful, or false if the conversion fails. This is safer than using Parse because it doesn't throw exceptions on invalid input. Syntax Following is the syntax for using TryParse − bool result = int.TryParse(stringValue, out int number); Parameters stringValue − The string to be parsed. out number − The output parameter that receives the parsed value if successful, or ...
Read MoreDirectoryNotFoundException in C#
The DirectoryNotFoundException in C# is thrown when an application attempts to access a directory that does not exist on the file system. This exception is part of the System.IO namespace and commonly occurs during file and directory operations. Syntax The exception is automatically thrown by the .NET framework when directory operations fail − public class DirectoryNotFoundException : SystemException Common methods that can throw this exception include − Directory.GetDirectories(path); Directory.GetFiles(path); DirectoryInfo directoryInfo = new DirectoryInfo(path); When DirectoryNotFoundException Occurs Here is an example that demonstrates when this exception occurs by trying ...
Read MoreHow to remove items from a list in C#?
In C#, there are several ways to remove items from a List. The most commonly used methods are Remove(), RemoveAt(), RemoveAll(), and Clear(). Each method serves different purposes depending on whether you want to remove by value, by index, by condition, or all items. Syntax Following are the common syntaxes for removing items from a list − // Remove by value list.Remove(item); // Remove by index list.RemoveAt(index); // Remove all items matching a condition list.RemoveAll(predicate); // Remove all items list.Clear(); Using Remove() Method The Remove() method removes the first occurrence ...
Read MoreHow to print the contents of array horizontally using C#?
Printing array contents horizontally in C# means displaying all elements in a single line, separated by spaces or other delimiters. This is useful for creating formatted output where elements appear side by side rather than vertically. When you print an array using a regular loop with Console.WriteLine(), each element appears on a new line. To display elements horizontally, C# provides several approaches. Syntax Using string.Join() method − string.Join(separator, array) Using Console.Write() in a loop − for (int i = 0; i < array.Length; i++) { Console.Write(array[i] + ...
Read MorePath methods in C#
To handle File Paths in C#, use the Path methods. These methods come under the System.IO namespace and provide a reliable way to work with file and directory paths across different operating systems. The Path class contains static methods that perform common operations on strings that contain file or directory path information. These methods handle path separators, invalid characters, and other platform-specific details automatically. Syntax Following is the syntax for commonly used Path methods − string extension = Path.GetExtension(path); string fileName = Path.GetFileName(path); string fileNameWithoutExt = Path.GetFileNameWithoutExtension(path); string directoryName = Path.GetDirectoryName(path); string fullPath = Path.GetFullPath(path); ...
Read More