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 143 of 196
C# 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 MoreC# Program to remove whitespaces in a string
In C#, there are several ways to remove whitespaces from a string. You can remove all spaces, only leading and trailing spaces, or all types of whitespace characters including tabs and newlines. Using String.Replace() Method The Replace()
Read MoreHow to get complete drive information using C#?
Drive information of an Operating System includes key details about storage drives such as drive name, volume label, free space, total size, drive format, and drive type. In C#, you can retrieve this information using the DriveInfo class from the System.IO namespace. Syntax Following is the syntax for creating a DriveInfo object and accessing its properties − DriveInfo driveInfo = new DriveInfo("DriveLetter"); string name = driveInfo.Name; long freeSpace = driveInfo.AvailableFreeSpace; DriveType type = driveInfo.DriveType; DriveInfo Properties Property Description Name Gets the drive name (e.g., "C:") ...
Read More