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 140 of 196
C# Program to get the last access time of a file
To get the last access time of a file in C#, you can use the LastAccessTime property of the FileInfo class or the static File.GetLastAccessTime() method. The last access time represents when the file was last opened or read. Syntax Using the FileInfo class − FileInfo fileInfo = new FileInfo("filepath"); DateTime lastAccess = fileInfo.LastAccessTime; Using the static File.GetLastAccessTime() method − DateTime lastAccess = File.GetLastAccessTime("filepath"); Using FileInfo Class The FileInfo class provides an object-oriented approach to working with files. Here's how to get the last access time − ...
Read MoreGet the creation time of a file in C#
To get the creation time of a file in C#, you can use the CreationTime property of the FileInfo class or the static methods from the File class. This allows you to retrieve when a file was originally created on the file system. Syntax Using the FileInfo class − FileInfo fileInfo = new FileInfo("filename.txt"); DateTime creationTime = fileInfo.CreationTime; Using the static File class − DateTime creationTime = File.GetCreationTime("filename.txt"); Using FileInfo Class The FileInfo class provides an object-oriented approach to work with file information. Create a FileInfo object and access ...
Read MoreLowercase suffixes in C#
In C#, lowercase suffixes are used with numeric literals to specify their data type explicitly. These suffixes tell the compiler to treat the literal as a specific numeric type rather than inferring the type automatically. Syntax Following are the common lowercase suffixes used with numeric literals − long number = 12345l; // l for long float number = 3.14f; // f for float uint number = 100u; // u for unsigned int ulong number = 500ul; ...
Read MoreLiteral number suffixes in C#
Literal number suffixes in C# are used to explicitly specify the data type of numeric literals. Without suffixes, the compiler infers the type based on the value, but suffixes ensure the literal is treated as a specific numeric type. These suffixes are particularly useful when working with method overloads, preventing ambiguous type conversions, and ensuring the correct data type is used for calculations. Syntax Following is the syntax for using literal number suffixes − dataType variable = numericValue + suffix; The suffix can be either uppercase or lowercase, but uppercase is recommended for ...
Read MoreElementAt() method in C#
The ElementAt() method in C# is a LINQ extension method that retrieves an element at a specified index from any collection that implements IEnumerable. It provides a way to access elements by index in collections that don't have built-in indexing support. Syntax Following is the syntax for the ElementAt() method − public static TSource ElementAt(this IEnumerable source, int index) Parameters source − The IEnumerable to return an element from. index − The zero-based index of the element to retrieve. Return Value Returns the element at the specified position in ...
Read MoreC# Program to get the first three elements from a list
The Take() method in C# is a LINQ extension method used to retrieve a specified number of elements from the beginning of a collection. It returns an IEnumerable containing the first n elements from the original collection. Syntax Following is the syntax for using the Take() method − IEnumerable Take(int count) Parameters count − An integer specifying the number of elements to return from the start of the sequence. Return Value The Take() method returns an IEnumerable that contains the specified number of elements from the start ...
Read MoreC# Program to display a string in reverse alphabetic order
To display a string in reverse order, you can convert the string to a character array and then use the Array.Reverse() method. This approach reverses the order of characters, displaying them from last to first. Syntax Following is the syntax for converting a string to character array − char[] arr = str.ToCharArray(); Following is the syntax for reversing the array − Array.Reverse(arr); Using Array.Reverse() Method The simplest approach is to convert the string to a character array and use Array.Reverse() to reverse the order of characters − ...
Read MoreC# Program to split a string on spaces
In C#, the Split() method is used to divide a string into an array of substrings based on a specified delimiter. When splitting a string on spaces, we use the space character ' ' as the delimiter. Syntax Following is the syntax for splitting a string on spaces − string[] result = str.Split(' '); You can also use the overloaded version with options − string[] result = str.Split(' ', StringSplitOptions.RemoveEmptyEntries); Parameters separator − The character used to split the string (space character ' ' in this case). ...
Read MoreC# Program to split parts in a Windows directory
A Windows directory path consists of multiple parts separated by backslashes (\). In C#, you can split a directory path into its individual components using the Split() method with the backslash character as the delimiter. This technique is useful for extracting specific parts of a path, validating directory structures, or processing file paths programmatically. Syntax Following is the syntax for splitting a Windows directory path − string[] parts = directoryPath.Split(''); You can also use the verbatim string literal to define the path − string path = @"C:\Users\Documents\MyFile.txt"; Using Split() ...
Read MoreC# Program to write a number in hexadecimal format
Converting numbers to hexadecimal format in C# can be accomplished using various format specifiers. Hexadecimal is a base-16 number system commonly used in programming for representing binary data in a more readable format. Syntax Following are the main hexadecimal format specifiers in C# − {0:x} // lowercase hexadecimal {0:X} // uppercase hexadecimal {0:x8} // lowercase with 8 digits (zero-padded) {0:X8} // uppercase with 8 digits (zero-padded) You can also use the ToString() method with format specifiers − number.ToString("x") // ...
Read More