Csharp Articles

Page 95 of 196

How to declare a tuple in C#?

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 548 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 343 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 434 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 323 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 504 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 618 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

What are postfix operators in C#?

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

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 More

What does the interface ICollection do in C#

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

The ICollection interface in C# defines the size, enumerators, and synchronization methods for all nongeneric collections. It is the base interface for classes in the System.Collections namespace and serves as the foundation for collection types like ArrayList, Hashtable, and Queue. This interface provides essential functionality that all collection classes must implement, including methods to copy elements and enumerate through the collection, along with properties to get the count and synchronization object. Syntax Following is the syntax for implementing the ICollection interface − public interface ICollection : IEnumerable { int Count { ...

Read More
Showing 941–950 of 1,951 articles
« Prev 1 93 94 95 96 97 196 Next »
Advertisements