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 114 of 196
Inbuilt Data Structures in C#
C# provides several powerful built-in data structures that make it easier to store, organize, and manipulate collections of data. These data structures are part of the .NET Framework and offer different capabilities for various programming scenarios. The most commonly used built-in data structures include List for dynamic arrays, ArrayList for non-generic collections, Dictionary for key-value pairs, and Queue and Stack for specialized ordering operations. List The generic List is a strongly-typed collection that can dynamically resize itself. Unlike arrays, you don't need to specify the size at compile time, and it provides better type safety compared to ...
Read MoreWhat is the difference between Read(), ReadKey() and ReadLine() methods in C#?
The Console class in C# provides three different methods for reading user input: Read(), ReadKey(), and ReadLine(). Each method serves a specific purpose and returns different types of data from the standard input stream. Syntax Following are the syntaxes for the three input methods − int result = Console.Read(); // Returns ASCII value ConsoleKeyInfo keyInfo = Console.ReadKey(); // Returns key information string input = Console.ReadLine(); // Returns string Console.Read() Method The Read() method reads the next character from the ...
Read MoreInfinity or Exception in C# when divide by 0?
When dividing by zero in C#, the behavior depends on the data type being used. Integer division by zero throws a DivideByZeroException, while floating-point division by zero returns Infinity or NaN (Not a Number) without throwing an exception. Syntax Following is the basic division syntax that can result in divide-by-zero scenarios − result = dividend / divisor; Exception handling syntax for integer division − try { result = dividend / divisor; } catch (DivideByZeroException ex) { // handle exception } Integer Division ...
Read MoreWhat is the difference between initialization and assignment of values in C#?
In C#, initialization and assignment are two distinct concepts that are often confused. Understanding the difference is crucial for effective programming and memory management. Declaration vs Initialization vs Assignment Declaration creates a variable name, initialization allocates memory and sets initial values, and assignment changes the value of an already existing variable. Declaration → Initialization → Assignment Declaration int[] n; Creates variable name Initialization n = new int[5]; Allocates memory Assignment ...
Read MoreWhat is the difference between overriding and hiding in C#?
In C#, method overriding and method hiding (also called shadowing) are two different mechanisms for redefining parent class methods in a derived class. Method overriding uses the override keyword and provides polymorphic behavior, while method hiding uses the new keyword and creates a separate method that shadows the parent method. Syntax Following is the syntax for method overriding using the override keyword − public class BaseClass { public virtual void Method() { } } public class DerivedClass : BaseClass { public override void Method() { } } ...
Read MoreAbort in C#
The Thread.Abort() method in C# is used to forcibly terminate a thread by throwing a ThreadAbortException. This method was commonly used in older versions of .NET Framework but is now deprecated and not supported in .NET Core and .NET 5+. Important Note: Thread.Abort() is considered unsafe and unreliable. Modern C# applications should use cooperative cancellation with CancellationToken instead. Syntax Following is the syntax for using Thread.Abort() − thread.Abort(); The method throws a ThreadAbortException that can be caught, but the thread will still terminate − try { // thread ...
Read MoreRetrieve data value as a pointer in C#
A pointer is a variable whose value is the address of another variable. In C#, pointers can only be used in unsafe contexts. To retrieve the data stored at the location referenced by the pointer variable, you can use the dereference operator * or call the ToString() method on the pointer. Syntax Following is the syntax for declaring and using pointers in C# − unsafe { int* ptr = &variable; // Declare pointer and get address int value = *ptr; ...
Read MoreSorting a HashMap according to keys in C#
In C#, the equivalent of Java's HashMap is the Dictionary class, which stores key-value pairs. Sorting a Dictionary by keys requires extracting the keys, sorting them, and then accessing the values in the sorted order. Syntax Following is the syntax for creating and sorting a Dictionary by keys − Dictionary dict = new Dictionary(); // Sort keys using LINQ var sortedByKeys = dict.OrderBy(x => x.Key); // Or extract and sort keys manually var keys = dict.Keys.ToList(); keys.Sort(); Using LINQ OrderBy Method The most efficient way to sort a Dictionary by keys ...
Read MoreChained Exceptions in C#
Chained exceptions in C# allow you to preserve the original exception information while throwing a new exception. This creates a chain of exceptions where each exception wraps the previous one, maintaining the complete error context throughout the call stack. When an exception occurs deep in your application, chained exceptions help maintain the full error history by wrapping the original exception inside a new one using the innerException parameter of the Exception constructor. Syntax Following is the syntax for creating a chained exception − try { // code that might throw exception } ...
Read MoreHow to call Math Operations using Delegates in C#?
To understand how to call Math Operations using Delegates in C#, let us see an example wherein we will perform mathematical operations like division, multiplication, and addition using delegates. A delegate in C# is a type that represents references to methods with a specific signature. Syntax Following is the syntax for declaring a delegate that can reference mathematical methods − delegate returnType DelegateName(parameters); Following is the syntax for assigning methods to a delegate − DelegateName delegateInstance = MethodName; // or DelegateName delegateInstance = new DelegateName(MethodName); Using Delegates for Basic Math ...
Read More