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 125 of 196
How to join or concatenate two lists in C#?
In C#, there are multiple ways to join or concatenate two lists. The most common approaches include using the AddRange() method, LINQ's Concat() method, and the Union() method. Each method has different behaviors and use cases. Syntax Following is the syntax for using AddRange() to concatenate lists − list1.AddRange(list2); Following is the syntax for using LINQ Concat() method − var result = list1.Concat(list2).ToList(); Following is the syntax for using Union() to join lists without duplicates − var result = list1.Union(list2).ToList(); Using AddRange() Method The AddRange() ...
Read MoreHow to list down all the files available in a directory using C#?
In C#, you can list all files in a directory using the DirectoryInfo class and its GetFiles() method. This approach provides detailed information about each file, including name, size, and other properties. Syntax Following is the syntax for creating a DirectoryInfo object and getting files − DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\path\to\directory"); FileInfo[] files = directoryInfo.GetFiles(); You can also specify search patterns and options − FileInfo[] files = directoryInfo.GetFiles("*.txt", SearchOption.TopDirectoryOnly); Using DirectoryInfo.GetFiles() The DirectoryInfo class provides detailed file information and is ideal when you need file properties like size, creation ...
Read MoreHow to loop through all values of an enum in C#?
In C#, you can loop through all values of an enum using the Enum.GetValues() method. This method returns an array containing all the values defined in the enum, which you can then iterate over using a foreach loop. Syntax Following is the syntax for looping through enum values − foreach (EnumType value in Enum.GetValues(typeof(EnumType))) { // Process each enum value } You can also use the generic version for type safety − foreach (EnumType value in Enum.GetValues()) { // Process each enum value (C# ...
Read MoreC# Program to check if a number is Positive, Negative, Odd, Even, Zero
In C#, you can determine if a number is positive, negative, odd, even, or zero using simple conditional statements and arithmetic operations. This involves checking the sign of the number and using the modulus operator to determine if a number is divisible by 2. Logic for Number Classification To classify a number, we use the following approaches − Positive/Negative/Zero: Compare the number with zero using relational operators. Odd/Even: Use the modulus operator (%) to check if the remainder when divided by 2 is zero. Number Classification Logic ...
Read MoreRecommended IDEs for C# on Windows/Linux/Mac OS
Choosing the right IDE is crucial for productive C# development. While Microsoft Visual Studio remains the gold standard on Windows, developers have excellent options across all major operating systems including Windows, Linux, and macOS. Microsoft Visual Studio (Windows) Visual Studio is the flagship IDE for C# development on Windows, offering comprehensive tools for building desktop, web, and mobile applications. Visual Studio Features IntelliSense Code completion Error detection Quick fixes Debugging Breakpoints Variable inspection ...
Read MoreHow to initialize a string to an empty string in C#?
In C#, there are several ways to initialize a string to an empty string. Understanding the different approaches helps you choose the most appropriate method for your specific use case. Syntax Following are the different ways to initialize a string to an empty string − string myStr = ""; // Empty string literal string myStr = string.Empty; // Using string.Empty property string myStr = null; // Null reference (not ...
Read MoreHow to initialize a tuple to an empty tuple in C#?
To initialize a tuple to an empty tuple in C#, you can declare a tuple variable without assigning it a value, or assign it to null. C# provides multiple ways to work with empty or uninitialized tuples depending on your specific requirements. Syntax Following are the different ways to declare an empty tuple − // Declare without initialization (default to null) Tuple myTuple; // Initialize to null explicitly Tuple myTuple = null; // Create tuple with null/default values Tuple myTuple = new Tuple(0, null); Using Uninitialized Tuple Declaration When you declare ...
Read MoreHow to get last 4 characters from string innC#?
Getting the last 4 characters from a string in C# is a common task that can be accomplished using the Substring() method. This method extracts a portion of the string based on the starting position and length specified. Syntax Following is the syntax for using Substring() to get the last characters − string.Substring(startIndex) string.Substring(startIndex, length) To get the last 4 characters, calculate the starting position as − str.Substring(str.Length - 4) Using Substring() Method The Substring() method extracts characters from a specified starting position to the end of the string. ...
Read MoreHow to insert an item in a list at a given position in C#?
To insert an item in an already created List, use the Insert() method. This method allows you to insert an element at a specific index position within the list, shifting all subsequent elements to the right. Syntax Following is the syntax for the Insert() method − list.Insert(index, item); Parameters index − The zero-based index at which the item should be inserted. item − The object to insert into the list. Insert Operation at Index 3 Before Insert: ...
Read MoreHow to join two lists in C#?
To join two lists in C#, you can use several methods. The most common approaches are using the AddRange() method to modify an existing list, or using LINQ's Concat() method to create a new combined list without modifying the originals. Syntax Using AddRange() to modify the first list − list1.AddRange(list2); Using LINQ Concat() to create a new list − var combinedList = list1.Concat(list2).ToList(); Using AddRange() Method The AddRange() method adds all elements from the second list to the end of the first list, modifying the original list − ...
Read More