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
Programming Articles
Page 877 of 2547
How to check if an item exists in a C# list collection?
In C#, you can check if an item exists in a List collection using several methods. The most common approach is the Contains() method, which returns true if the item is found and false otherwise. Syntax Following is the syntax for the Contains() method − bool Contains(T item) Parameters item − The object to locate in the list. The value can be null for reference types. Return Value Returns true if the item is found in the list; otherwise, false. Using Contains() Method Example using ...
Read MoreString Formatting in C# to add padding on the right
String formatting in C# provides several ways to add padding to strings. Right padding aligns text to the left by adding spaces to the right side of the string until it reaches the specified width. Syntax Following is the syntax for right padding using composite formatting − string.Format("{0, -width}", value) Following is the syntax for right padding using string interpolation − $"{value, -width}" Following is the syntax for right padding using the PadRight() method − string.PadRight(totalWidth) string.PadRight(totalWidth, paddingChar) Right Padding Visualization ...
Read MoreSet tuple as a method parameter in C#
In C#, you can pass tuples as method parameters to group related values together. This approach is useful when you need to pass multiple values to a method without creating a separate class or using multiple parameters. Syntax Following is the syntax for creating a tuple and passing it as a method parameter − // Creating a tuple var tuple = Tuple.Create(value1, value2, value3); // Method with tuple parameter static void MethodName(Tuple tuple) { // Access tuple items using Item1, Item2, Item3 } Using Classic Tuple as Method Parameter ...
Read MoreDate Class in C#
The DateTime class in C# is used to work with dates and times. It represents an instant in time, ranging from 12:00:00 midnight, January 1, 0001 to 11:59:59 P.M., December 31, 9999 A.D. The DateTime class provides various methods and properties to create, manipulate, and format date and time values. Syntax Following is the syntax for creating a DateTime object − DateTime variableName = new DateTime(year, month, day); DateTime variableName = new DateTime(year, month, day, hour, minute, second); To get the current date and time − DateTime current = DateTime.Now; ...
Read MoreType.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 More