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
Csharp Articles
Page 91 of 196
Why we do not have global variables in C#?
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 MoreHow to define multiline String Literal in C#?
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 MoreHow to call a method of a class in C#
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 MoreHow to capture divide by zero exception in C#?
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 MoreWhat are contextual keywords in C#?
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 MoreWhat are dynamic arrays in C#?
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 MoreWhat are the differences between constructors and destructors in C#?
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 MoreWhat are the differences between ref and out parameters in C#?
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 MoreWhat are the differences between class methods and class members in C#?
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 MoreWhat does the @ prefix do on string literals in C#?
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