Server Side Programming Articles

Page 770 of 2109

Ternary Operator in C#

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 3K+ Views

The ternary operator in C# is a conditional operator that provides a concise way to evaluate expressions based on a Boolean condition. It takes three operands and returns one of two values depending on whether the condition is true or false. The ternary operator is also known as the conditional operator and uses the syntax condition ? value_if_true : value_if_false. It's particularly useful for simple conditional assignments and can make code more readable when used appropriately. Syntax Following is the syntax for the ternary operator − result = condition ? value_if_true : value_if_false; ...

Read More

Push vs pop in stack class in C#

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 753 Views

The Stack class in C# represents a last-in, first-out (LIFO) collection of objects. It is used when you need to store and retrieve elements in reverse order — the last element added is the first one to be removed. The Stack class provides two fundamental operations: Push() to add elements and Pop() to remove elements from the top of the stack. Stack LIFO Operations D (Top) C B A ...

Read More

How to declare a tuple in C#?

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 552 Views

A tuple in C# is a data structure that can hold multiple values of different types. Tuples are useful when you need to return multiple values from a method or group related data together without creating a separate class. C# provides two main ways to declare tuples: the classic Tuple class and the newer value tuples introduced in C# 7.0 with cleaner syntax. Syntax Classic tuple syntax − Tuple tuple = new Tuple(value1, value2); Value tuple syntax (C# 7.0+) − (int, string) tuple = (value1, value2); // or with named elements ...

Read More

How to use #undef directive in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 352 Views

The #undef directive in C# allows you to undefine a previously defined symbol, making it unavailable for use in conditional compilation directives like #if. This is useful for controlling which code sections are included during compilation. Syntax Following is the syntax for using the #undef directive − #undef SYMBOL Where SYMBOL is the identifier you want to undefine. For example − #undef DEBUG #undef TESTING Key Rules The #undef directive must appear at the top of the file, before any code or using statements. You ...

Read More

How to capture null reference exception in C#?

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 446 Views

A NullReferenceException occurs when you attempt to access a member (method, property, or field) of a null object reference. This is one of the most common runtime exceptions in C#. What Causes NullReferenceException? The exception is thrown when you try to access members of an object that has not been initialized or has been set to null − string str = null; int length = str.Length; // Throws NullReferenceException Using Null Checks The most straightforward way to prevent NullReferenceException is to check for null before accessing object members − using System; ...

Read More

How to demonstrate Prefix Operator using C#?

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 326 Views

The prefix operator in C# is used to increment or decrement a variable's value before using it in an expression. The prefix increment operator ++ increases the value by 1, while the prefix decrement operator -- decreases the value by 1. The key characteristic is that the operation happens before the variable's value is returned or used. Syntax Following is the syntax for prefix increment and decrement operators − ++variable; // Prefix increment --variable; // Prefix decrement The operators can also be used in expressions − result = ++a; ...

Read More

How to declare a two-dimensional array in C#

George John
George John
Updated on 17-Mar-2026 1K+ Views

A two-dimensional array in C# is a data structure that stores elements in a grid-like format with rows and columns. It can be thought of as an array of arrays, where each element is accessed using two indices. Syntax Following is the syntax for declaring a two-dimensional array − datatype[, ] arrayName; Following is the syntax for initializing a two-dimensional array − datatype[, ] arrayName = new datatype[rows, columns]; You can also declare and initialize in one statement − datatype[, ] arrayName = new datatype[rows, columns] { ...

Read More

What does the keyword var do in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 508 Views

The var keyword in C# enables implicit type declaration where the compiler automatically determines the variable's type based on the assigned value. This feature was introduced in C# 3.0 and provides cleaner code while maintaining type safety. Syntax Following is the syntax for using var keyword − var variableName = initialValue; The compiler infers the type from the initial value. Once declared, the variable behaves as if it were declared with its actual type. How It Works When you use var, the C# compiler performs type inference at compile time. The variable ...

Read More

How to use #error and #warning directives in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 625 Views

The #error and #warning directives in C# are preprocessor directives that allow developers to generate custom compiler errors and warnings during the compilation process. These directives are useful for conditional compilation scenarios and providing feedback about missing configurations or deprecated code paths. Syntax Following is the syntax for the #error directive − #error error-message Following is the syntax for the #warning directive − #warning warning-message Using #error Directive The #error directive generates a compile-time error with a custom message. This prevents the code from compiling and forces the developer ...

Read More

How to use #if..#elif...#else...#endif directives in C#?

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 1K+ Views

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 More
Showing 7691–7700 of 21,090 articles
« Prev 1 768 769 770 771 772 2109 Next »
Advertisements