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
Server Side Programming Articles
Page 817 of 2109
Manipulate decimals with numeric operators in C#
The decimal data type in C# provides precise arithmetic operations for financial and monetary calculations. You can manipulate decimals using standard numeric operators such as +, -, *, /, and %. Decimal literals in C# must be suffixed with M or m to distinguish them from double values. This ensures precision is maintained during calculations. Syntax Following is the syntax for declaring decimal variables − decimal variableName = value M; Following is the syntax for basic arithmetic operations with decimals − decimal result = decimal1 + decimal2; // Addition decimal ...
Read MoreC# Program to display temporary file names
The Path.GetTempPath() method in C# retrieves the path of the system's temporary folder where temporary files are stored. This method is useful when your application needs to create temporary files or work with the system's designated temporary directory. Syntax Following is the syntax for using Path.GetTempPath() method − string tempPath = Path.GetTempPath(); Return Value The method returns a string representing the path to the system's temporary directory. On Windows, this is typically the path specified by the TEMP or TMP environment variables. On Unix-like systems, it's usually /tmp/. Using GetTempPath() to Display ...
Read MoreC# Program to generate random lowercase letter
In C#, you can generate a random lowercase letter using the Random class and ASCII character conversion. This technique generates a random number between 0 and 25, then converts it to a corresponding lowercase letter from 'a' to 'z'. Syntax Following is the syntax for generating a random lowercase letter − Random random = new Random(); int randomIndex = random.Next(0, 26); char randomLetter = (char)('a' + randomIndex); How It Works The process involves three key steps: Generate random number: random.Next(0, 26) produces a number from 0 to 25 ...
Read MoreC# Program to match all the digits in a string
To match all digits in a string, we can use regular expressions (Regex) in C#. Regular expressions provide a powerful way to search for patterns in text, including numeric values. The System.Text.RegularExpressions namespace contains the Regex class that allows us to define patterns and find matches within strings. Syntax Following is the syntax for matching digits using Regex − MatchCollection matches = Regex.Matches(inputString, @"\d+"); The regular expression pattern \d+ breaks down as follows − \d − matches any single digit (0-9) + − matches one or more consecutive occurrences ...
Read MoreReplace parts of a string with C# Regex
Regular expressions (Regex) in C# provide powerful pattern matching and string replacement capabilities. The Regex.Replace() method allows you to replace parts of a string that match a specified pattern with a replacement string. Syntax Following is the syntax for using Regex.Replace() method − Regex.Replace(input, pattern, replacement); Parameters input − The string to search for a match pattern − The regular expression pattern to match replacement − The replacement string Return Value Returns a new string where all matches of the pattern are replaced with the replacement string. If no ...
Read MoreC# program to get the extension of a file in C#
The Path.GetExtension() method in C# is used to extract the file extension from a file path. This method is part of the System.IO namespace and returns the extension portion of the path string, including the period (.). Syntax Following is the syntax for using Path.GetExtension() method − public static string GetExtension(string path) Parameters path: A string containing the file path from which to get the extension. Return Value The method returns a string containing the extension of the specified path (including the period), or an empty string if the ...
Read MoreC# program to get the file name in C#
In C#, you can extract the file name from a full path using the Path.GetFileName() method from the System.IO namespace. This method returns only the file name and extension, without the directory path. Syntax Following is the syntax for using Path.GetFileName() method − string fileName = Path.GetFileName(filePath); Parameters filePath − A string containing the full path to the file. Return Value The method returns a string containing the file name and extension. If the path ends with a directory separator, it returns an empty string. Using ...
Read MoreEnvironment.NewLine in C#
The Environment.NewLine property in C# provides a platform-independent way to add line breaks in strings. It automatically returns the appropriate newline character sequence for the current operating system − \r on Windows and on Unix-based systems. Using Environment.NewLine ensures your code works correctly across different platforms without hardcoding specific line break characters. Syntax Following is the syntax for using Environment.NewLine − string result = text1 + Environment.NewLine + text2; You can also use it in string concatenation or interpolation − string result = $"Line 1{Environment.NewLine}Line 2"; Basic Usage ...
Read MoreEnumerateFiles method in C#
The Directory.EnumerateFiles() method in C# is used to enumerate all files in a specified directory. It returns an IEnumerable collection of file paths, making it memory-efficient for large directories as it processes files lazily (one at a time) rather than loading all file paths into memory at once. Syntax Following is the basic syntax for the Directory.EnumerateFiles() method − Directory.EnumerateFiles(path) Directory.EnumerateFiles(path, searchPattern) Directory.EnumerateFiles(path, searchPattern, searchOption) Parameters path − The directory path to search for files. searchPattern − The search string to match file names (e.g., "*.*" for all files, "*.txt" for text ...
Read MoreFunc generic type in C#
The Func generic type in C# is a built-in delegate that represents a method which takes zero or more input parameters and returns a value. It provides a convenient way to store anonymous methods, lambda expressions, and regular methods that return a value. Syntax Following is the syntax for declaring a Func delegate − Func funcName; // No parameters, returns TResult Func funcName; ...
Read More