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
Type.GetEnumName() Method in C#
The Type.GetEnumName() method in C# returns the name of the constant that has the specified value for the current enumeration type. This method is useful when you need to convert an enum value back to its string representation. Syntax Following is the syntax − public virtual string GetEnumName(object value); Parameters value − An object whose value is a constant in the current enumeration type. Return Value Returns a string containing the name of the enumerated constant that has the specified value, or null if no such constant is found. ...
Read MoreHow to use #if..#elif...#else...#endif directives in C#?
Preprocessor directives in C# begin with # and are processed before compilation. The #if..#elif...#else...#endif directives allow conditional compilation, meaning you can include or exclude code blocks based on defined symbols. These directives are useful for creating debug builds, platform-specific code, or feature toggles without affecting runtime performance. Syntax Following is the syntax for conditional compilation directives − #if condition // code block 1 #elif condition // code block 2 #else // code block 3 #endif Key Directives #if − Tests if ...
Read MoreHow to define character constants in C#?
Character constants in C# are single characters enclosed in single quotes. They can be stored in variables of type char and represent individual characters, escape sequences, or Unicode characters. Syntax Following is the syntax for defining character constants − char variableName = 'character'; Character constants can be − Plain characters: 'a', 'X', '5' Escape sequences: '', '\t', ''' Unicode characters: '\u0041' (represents 'A') Common Character Constants Here are the most commonly used character escape sequences − Escape Sequence Character Description ...
Read MoreHow do you make code reusable in C#?
To make code reusable in C#, there are several key approaches including interfaces, inheritance, generic classes, and static methods. Among these, interfaces are particularly powerful as they define a contract that multiple classes can implement, enabling polymorphism and flexible code design. Interfaces define properties, methods, and events without providing implementations. The implementing classes must provide the actual functionality, ensuring a consistent structure across different implementations. Syntax Following is the syntax for declaring an interface − public interface IInterfaceName { void MethodName(); int PropertyName { get; set; } } ...
Read MoreString Formatting with ToString in C#
The ToString() method in C# is a powerful way to format values into strings using various format specifiers. It provides control over how numbers, dates, and other data types are displayed as text. Syntax Following is the basic syntax for using ToString() with format specifiers − variable.ToString("formatSpecifier"); For numeric formatting with custom patterns − number.ToString("000"); // Zero padding number.ToString("C"); // Currency format number.ToString("F2"); // Fixed-point with 2 decimals Using Zero Padding Format The zero padding format ensures ...
Read MoreReturn a C# tuple from a method
A tuple in C# is a data structure that can hold multiple values of different types. You can return a tuple from a method, which is useful when you need to return multiple values from a single method call. There are two main ways to return tuples from methods in C# − using the Tuple class and using the newer value tuples with more concise syntax. Syntax Using the Tuple class − static Tuple MethodName() { return Tuple.Create(value1, value2, value3); } Using value tuples (C# 7.0 and later) − ...
Read MoreCounters in C#
Counters in C# are performance counters that allow you to monitor your application's performance metrics in real-time. These counters provide valuable insights into system resources, application behavior, and overall performance characteristics. When building applications — whether web, mobile, or desktop — monitoring performance is crucial for identifying bottlenecks, optimizing resource usage, and ensuring smooth operation under various load conditions. Syntax Following is the syntax for creating a performance counter − PerformanceCounter counter = new PerformanceCounter(categoryName, counterName, instanceName); Following is the syntax for reading counter values − float value = counter.NextValue(); ...
Read MoreWhat are postfix operators in C#?
The postfix operators in C# are the increment (++) and decrement (--) operators that are placed after the variable. With postfix operators, the current value of the variable is returned first, and then the variable is incremented or decremented by 1. Syntax Following is the syntax for postfix increment and decrement operators − variable++; // postfix increment variable--; // postfix decrement How Postfix Operators Work The key behavior of postfix operators is that they return the original value before performing the increment or decrement operation. This is different from prefix operators ...
Read MoreHow to find a matching substring using regular expression in C#?
Regular expressions in C# provide a powerful way to search for specific patterns within strings. The Regex.Matches() method from the System.Text.RegularExpressions namespace allows you to find all occurrences of a pattern in a string. To find a matching substring, you create a regex pattern and use it to search through your target string. The pattern can be a simple literal match or include special regex metacharacters for more complex searches. Syntax Following is the basic syntax for finding matches using regular expressions − MatchCollection matches = Regex.Matches(inputString, pattern); For word boundary matching, use ...
Read MoreSwapping Characters of a String in C#
Swapping characters in a string involves replacing specific characters with their counterparts while preserving the original structure. In C#, this can be accomplished using various approaches including the Select method with LINQ, character arrays, or the Replace method. Syntax Following is the syntax for swapping characters using LINQ's Select method − string.Select(character => condition ? replacement : character).ToArray() Following is the syntax for swapping using character arrays − char[] charArray = string.ToCharArray(); // modify charArray elements new string(charArray) Using LINQ Select Method The Select method processes each character individually ...
Read More