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
Articles on Trending Technologies
Technical articles with clear explanations and examples
What does the @ prefix do on string literals in C#?
The @ prefix in C# creates a verbatim string literal, which means you don't need to escape special characters like backslashes, quotes, or newlines. This makes the string easier to read and write, especially for file paths, regular expressions, and multi-line text. Syntax Following is the syntax for verbatim string literals − @"string content here" To include a double quote inside a verbatim string, use two consecutive quotes − @"He said ""Hello"" to me" Using @ for File Paths The @ prefix eliminates the need to escape backslashes in ...
Read MoreWhat are tokens in C#?
A token is the smallest element of a C# program that the compiler can recognize. Tokens are the building blocks of C# code and include keywords, identifiers, literals, operators, and punctuation marks. Understanding tokens is fundamental to writing valid C# programs. Types of Tokens in C# Keywords int, if, class Reserved words Identifiers myVar, Main Names Literals 42, "hello" Values Operators ...
Read MoreHow to join or concatenate two lists in C#?
In C#, there are multiple ways to join or concatenate two lists. The most common approaches include using the AddRange() method, LINQ's Concat() method, and the Union() method. Each method has different behaviors and use cases. Syntax Following is the syntax for using AddRange() to concatenate lists − list1.AddRange(list2); Following is the syntax for using LINQ Concat() method − var result = list1.Concat(list2).ToList(); Following is the syntax for using Union() to join lists without duplicates − var result = list1.Union(list2).ToList(); Using AddRange() Method The AddRange() ...
Read MoreRound a number to the nearest even number in C#
The MidpointRounding.ToEven option is used with rounding methods in C# to round a number to the nearest even number when the fractional part is exactly 0.5. This is also known as banker's rounding or round half to even. Syntax Following is the syntax for rounding to the nearest even number − decimal.Round(value, digits, MidpointRounding.ToEven) Math.Round(value, digits, MidpointRounding.ToEven) Parameters value − The decimal or double number to be rounded digits − The number of decimal places in the return value MidpointRounding.ToEven − Rounds to the nearest even number ...
Read MoreC# DefaultIfEmpty Method
The DefaultIfEmpty method in C# is a LINQ extension method used to handle empty collections gracefully. Instead of returning an empty sequence that might cause issues in further operations, it returns a sequence containing a single default value when the original collection is empty. Syntax Following is the syntax for the DefaultIfEmpty method − public static IEnumerable DefaultIfEmpty( this IEnumerable source ) public static IEnumerable DefaultIfEmpty( this IEnumerable source, TSource defaultValue ) Parameters source − The sequence to return ...
Read MoreDateTime.ToFileTimeUtc() Method in C#
The DateTime.ToFileTimeUtc() method in C# converts the value of the current DateTime object to a Windows file time. A Windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (UTC). This method is particularly useful when working with file systems, as Windows uses this format internally to store file timestamps. The method automatically converts the DateTime to UTC before performing the conversion. Syntax Following is the syntax − public long ToFileTimeUtc(); Return Value This method returns a long ...
Read MoreHow to list down all the files available in a directory using C#?
In C#, you can list all files in a directory using the DirectoryInfo class and its GetFiles() method. This approach provides detailed information about each file, including name, size, and other properties. Syntax Following is the syntax for creating a DirectoryInfo object and getting files − DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\path\to\directory"); FileInfo[] files = directoryInfo.GetFiles(); You can also specify search patterns and options − FileInfo[] files = directoryInfo.GetFiles("*.txt", SearchOption.TopDirectoryOnly); Using DirectoryInfo.GetFiles() The DirectoryInfo class provides detailed file information and is ideal when you need file properties like size, creation ...
Read MoreDecimal type in C#
The decimal type in C# is a 128-bit data type designed for financial and monetary calculations that require high precision. Unlike float and double, the decimal type provides exact decimal representation without rounding errors, making it ideal for applications involving currency and precise arithmetic operations. Syntax Following is the syntax for declaring decimal variables − decimal variableName = value; decimal variableName = valueM; // M or m suffix required for literals The M or m suffix is required when assigning decimal literals to distinguish them from double values − decimal price = ...
Read MoreWhat is the difference between initialization and assignment of values in C#?
In C#, initialization and assignment are two distinct concepts that are often confused. Understanding the difference is crucial for effective programming and memory management. Declaration vs Initialization vs Assignment Declaration creates a variable name, initialization allocates memory and sets initial values, and assignment changes the value of an already existing variable. Declaration → Initialization → Assignment Declaration int[] n; Creates variable name Initialization n = new int[5]; Allocates memory Assignment ...
Read MoreHow to loop through all values of an enum in C#?
In C#, you can loop through all values of an enum using the Enum.GetValues() method. This method returns an array containing all the values defined in the enum, which you can then iterate over using a foreach loop. Syntax Following is the syntax for looping through enum values − foreach (EnumType value in Enum.GetValues(typeof(EnumType))) { // Process each enum value } You can also use the generic version for type safety − foreach (EnumType value in Enum.GetValues()) { // Process each enum value (C# ...
Read More