Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 29 of 151

DirectoryNotFoundException in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 922 Views

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 More

C# program to print all the numbers divisible by 3 and 5 for a given number

Samual Sam
Samual Sam
Updated on 17-Mar-2026 6K+ Views

To print numbers divisible by both 3 and 5, we use the modulus operator % with the logical AND operator &&. A number is divisible by both 3 and 5 if the remainder is zero when divided by each of these numbers. Syntax Following is the syntax to check if a number is divisible by both 3 and 5 − if (num % 3 == 0 && num % 5 == 0) { // number is divisible by both 3 and 5 } Alternatively, since a number divisible by both 3 ...

Read More

Compound assignment operators in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 4K+ Views

Compound assignment operators in C# provide a shorter syntax to perform an operation and assign the result back to the same variable. These operators combine arithmetic, bitwise, or shift operations with assignment in a single step. For example, x += 5 is equivalent to x = x + 5, but more concise and readable. Syntax The general syntax for compound assignment operators is − variable operator= value; This is equivalent to − variable = variable operator value; Types of Compound Assignment Operators Operator Name Equivalent ...

Read More

Path methods in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 340 Views

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

Default constructor in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 415 Views

A default constructor in C# is a constructor that takes no parameters. When you create an object of a class, the constructor is automatically invoked. The constructor has the same name as the class and is used to initialize the object. If you don't explicitly define any constructor in your class, C# automatically provides a default constructor. However, if you define any parameterized constructor, you must explicitly define the default constructor if you want to use it. Syntax Following is the syntax for declaring a default constructor − public class ClassName { ...

Read More

How to get complete drive information using C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 302 Views

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

Convert a ValueTuple to a Tuple in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 596 Views

In C#, you can easily convert a ValueTuple to a Tuple using the ToTuple() method. ValueTuples are value types introduced in C# 7.0, while Tuples are reference types that have been available since earlier versions of C#. Note − For .NET Framework projects, you may need to add the System.ValueTuple NuGet package to use ValueTuple features. Syntax Following is the syntax for converting a ValueTuple to a Tuple using the ToTuple() method − var valueTuple = (value1, value2, value3); Tuple tuple = valueTuple.ToTuple(); Converting ValueTuple to Tuple Example using System; ...

Read More

Difference between Boxing and Unboxing in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 1K+ Views

Boxing converts a value type to an object type, whereas unboxing converts an object type back to the value type. These operations are fundamental concepts in C# that involve moving data between the stack and heap memory. Boxing occurs implicitly when you assign a value type to an object variable, while unboxing requires explicit casting and can throw exceptions if the types don't match. Syntax Following is the syntax for boxing (implicit conversion) − object boxedValue = valueType; Following is the syntax for unboxing (explicit conversion) − ValueType unboxedValue = (ValueType)boxedObject; ...

Read More

C# Hexadecimal ("X") Format Specifier

Samual Sam
Samual Sam
Updated on 17-Mar-2026 9K+ Views

The hexadecimal ("X") format specifier is used to convert a number to a string of hexadecimal digits. The case of the format specifier determines whether hexadecimal digits greater than 9 are displayed in uppercase or lowercase. Use "X" for uppercase hexadecimal letters (A, B, C, D, E, F) and "x" for lowercase hexadecimal letters (a, b, c, d, e, f). Syntax Following is the syntax for using the hexadecimal format specifier − number.ToString("X") // uppercase number.ToString("x") // lowercase number.ToString("X4") // uppercase with minimum 4 ...

Read More

How to compare two arrays in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 2K+ Views

Comparing arrays in C# can be done using several approaches. The most common and efficient method is using the SequenceEqual() method from LINQ, but you can also compare arrays manually using loops or other techniques. Syntax Following is the syntax for using SequenceEqual() to compare two arrays − bool result = array1.SequenceEqual(array2); Following is the syntax for manual comparison using a loop − bool areEqual = true; for (int i = 0; i < array1.Length; i++) { if (array1[i] != array2[i]) { ...

Read More
Showing 281–290 of 1,507 articles
« Prev 1 27 28 29 30 31 151 Next »
Advertisements