Csharp Articles

Page 91 of 196

Why we do not have global variables in C#?

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

C# does not have global variables like those found in C or C++. Instead, C# follows an object-oriented paradigm where all data and methods must be contained within classes or structures. The global namespace alias (global::) is used to resolve naming conflicts between namespaces, not to access global variables. Why C# Doesn't Have Global Variables C# was designed with several principles that eliminate the need for global variables − Type Safety: Global variables can lead to unpredictable behavior and make debugging difficult. Object-Oriented Design: Everything must belong to a class or struct, promoting ...

Read More

How to define multiline String Literal in C#?

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

A multiline string literal in C# allows you to define strings that span multiple lines while preserving the line breaks and formatting. This is achieved using the @ symbol prefix, which creates a verbatim string literal. Syntax Following is the syntax for defining a multiline string literal − string variableName = @"Line 1 Line 2 Line 3"; The @ symbol tells the compiler to treat the string literally, preserving all whitespace, line breaks, and special characters without requiring escape sequences. Using Verbatim String Literals Let's say you want to create a string ...

Read More

How to call a method of a class in C#

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

To call a method of a class in C#, you need to create an instance of the class first, then use the dot notation to access its methods. The general syntax is objectName.MethodName(parameters). Syntax Following is the syntax for calling an instance method − ClassName objectName = new ClassName(); returnType result = objectName.MethodName(parameters); For static methods, you call them directly on the class without creating an instance − returnType result = ClassName.StaticMethodName(parameters); Using Instance Methods Instance methods require an object of the class to be called. Here's how you ...

Read More

How to capture divide by zero exception in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 4K+ Views

The System.DivideByZeroException is a class that handles errors generated from dividing a number by zero. This exception occurs when you attempt to divide an integer or decimal value by zero during arithmetic operations. In C#, you can capture and handle this exception using try-catch blocks to prevent your application from crashing and provide meaningful error messages to users. Syntax Following is the syntax for handling divide by zero exception − try { result = dividend / divisor; } catch (DivideByZeroException e) { // Handle the exception ...

Read More

What are contextual keywords in C#?

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

In C#, contextual keywords are special identifiers that have reserved meaning only in specific contexts. Unlike regular keywords, they can still be used as variable names or identifiers when not in their special context. Common examples include get and set in properties, where in LINQ queries, and partial in class definitions. Contextual keywords provide flexibility by allowing the same word to serve as both a keyword and an identifier depending on the context in which it appears. Syntax Contextual keywords are used within their specific contexts. Here are some common patterns − // Property accessors ...

Read More

What are dynamic arrays in C#?

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

Dynamic arrays are growable arrays that can resize themselves during runtime, providing a significant advantage over static arrays which have a fixed size. In C#, you can create dynamic arrays using collections like ArrayList and the more modern List. These dynamic collections allow automatic memory allocation, resizing, adding, searching, and sorting items efficiently. The ArrayList stores objects of any type, while List is type-safe and performs better. Syntax Following is the syntax for creating an ArrayList − ArrayList arrayList = new ArrayList(); arrayList.Add(item); Following is the syntax for creating a generic List − ...

Read More

What are the differences between constructors and destructors in C#?

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

A constructor is a special member function that initializes objects when they are created, while a destructor is called when an object goes out of scope or is garbage collected. Understanding the differences between these two fundamental concepts is essential for proper object lifecycle management in C#. Constructors A constructor is a special method that is automatically called when an object is created. It has the same name as the class and no return type. Syntax class ClassName { public ClassName() { // constructor code ...

Read More

What are the differences between ref and out parameters in C#?

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

The ref and out parameters in C# are both used to pass arguments by reference, but they have distinct behaviors and use cases. Both allow methods to modify the original variable, but they differ in initialization requirements and intended purposes. Syntax Following is the syntax for declaring ref parameters − public void MethodName(ref int parameter) { // parameter can be read and modified } Following is the syntax for declaring out parameters − public void MethodName(out int parameter) { // parameter must be assigned ...

Read More

What are the differences between class methods and class members in C#?

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

In C#, understanding the difference between class members and class methods is fundamental to object-oriented programming. Class members are the data components that store the state of an object, while class methods are the functions that operate on that data and define the object's behavior. Class Members vs Class Methods Class Members Class Methods Store data and represent the state of an object Define behavior and operations on the object's data Examples: fields, properties, constants Examples: functions, procedures, constructors Hold values that can change over time Execute ...

Read More

What does the @ prefix do on string literals in C#?

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

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 More
Showing 901–910 of 1,951 articles
« Prev 1 89 90 91 92 93 196 Next »
Advertisements