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 167 of 196
Check if a File is hidden in C#
To check if a file is hidden in C#, use the FileAttributes enumeration which contains various file attribute members like Compressed, Directory, Hidden, and others. The FileAttributes.Hidden flag indicates whether a file has the hidden attribute set. You can retrieve file attributes using File.GetAttributes() and then check for the Hidden flag using bitwise operations. Syntax Following is the syntax for getting file attributes − FileAttributes attributes = File.GetAttributes(filePath); Following is the syntax for checking if a file is hidden − bool isHidden = (attributes & FileAttributes.Hidden) == FileAttributes.Hidden; Using ...
Read MoreCheck if a File exists in C#
The File.Exists() method in C# is used to check whether a file exists at a specified path. This method returns true if the file exists, and false if it does not. It is part of the System.IO namespace and provides a simple way to verify file existence before performing file operations. Syntax Following is the syntax for the File.Exists() method − public static bool Exists(string path) Parameters path − The file path to check. Can be a relative path (current directory) or absolute path (full path including drive). Return Value ...
Read MoreWhat is the IsFixedSize property of Hashtable class in C#?
The IsFixedSize property of the Hashtable class in C# returns a bool value indicating whether the Hashtable has a fixed size. When this property returns false, you can add or remove elements. When it returns true, the size is fixed and you cannot add or remove elements. For a regular Hashtable created using the default constructor, IsFixedSize always returns false, meaning it can grow dynamically as you add elements. Syntax Following is the syntax to check if a Hashtable has a fixed size − bool isFixed = hashtable.IsFixedSize; Return Value The IsFixedSize ...
Read MoreCheck if both halves of the string have same set of characters in C#
To check if both halves of a string have the same set of characters in C#, we need to split the string into two equal halves and compare the character frequencies in each half. This problem is useful for validating palindromic properties and string pattern matching. Algorithm The approach uses character frequency counting − Create two frequency arrays to count characters in each half Iterate from both ends of the string toward the center Count character occurrences in the left half and right half Compare the frequency arrays to determine if both halves contain the same ...
Read Morechar vs string keywords in C#
The char and string keywords in C# are used to work with textual data, but they serve different purposes. The char keyword represents a single character, while string represents a sequence of characters. Understanding the difference between these two data types is essential for effective text manipulation in C#. Syntax Following is the syntax for declaring a char variable − char variableName = 'A'; // Single quotes for character Following is the syntax for declaring a string variable − string variableName = "Hello"; // Double quotes for string ...
Read MoreComparing enum members in C#
To compare enum members in C#, you can use the Enum.CompareTo() method or standard comparison operators. Enum comparison is based on the underlying numeric values assigned to each enum member. Syntax Following is the syntax for using CompareTo() method − enumValue1.CompareTo(enumValue2) Following is the syntax for using comparison operators − enumValue1 == enumValue2 // equality enumValue1 > enumValue2 // greater than enumValue1 < enumValue2 // less than Return Value The CompareTo() method returns − Negative value if the first enum is ...
Read MoreCompute modulus division by a power-of-2-number in C#
Computing modulus division by a power-of-2 number in C# can be optimized using bitwise operations. When the divisor is a power of 2 (like 2, 4, 8, 16, etc.), we can use the bitwise AND operation with (divisor - 1) instead of the traditional modulus operator for better performance. This optimization works because powers of 2 in binary have only one bit set, and subtracting 1 creates a mask that isolates the relevant lower bits. Syntax Following is the syntax for computing modulus with a power-of-2 divisor − result = dividend & (divisor - 1); ...
Read MoreCompressing and Decompressing files in C#
File compression and decompression in C# is accomplished using the System.IO.Compression namespace. This namespace provides classes like GZipStream and ZipArchive for handling compressed files efficiently. Syntax Following is the syntax for compressing files using GZipStream − using (var compressStream = new GZipStream(outputStream, CompressionMode.Compress)) { // write data to compress } Following is the syntax for decompressing files using GZipStream − using (var decompressStream = new GZipStream(inputStream, CompressionMode.Decompress)) { // read decompressed data } Using GZipStream for File Compression To compress a ...
Read MoreConst vs Static vs Readonly in C#
The const, static, and readonly keywords in C# serve different purposes for declaring fields and members. Understanding their differences is crucial for choosing the right approach based on your specific needs. Const Constant fields are compile-time constants that cannot be modified after declaration. They must be assigned a value at the time of declaration and are implicitly static. const int a = 5; Example using System; class Constants { public const int MaxValue = 100; public const string AppName = "MyApp"; public ...
Read MoreCall a method Asynchronously in C#
Asynchronous programming in C# is an efficient approach for handling operations that may be blocked or delayed. In synchronous processing, if an operation is blocked, the entire application waits, causing it to become unresponsive and take longer to complete tasks. Using the asynchronous approach, applications can continue executing other tasks while waiting for blocked operations to complete. This is especially important in GUI applications where blocking the main thread causes the interface to freeze. The Problem with Synchronous Code In GUI applications, when synchronous operations take too long, the application shows "not responding" messages because the main ...
Read More