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
Programming Articles
Page 825 of 2547
Why do we use comma operator in C#?
The comma operator in C# serves as a separator and allows multiple operations within a single statement. It is most commonly used in for loops for multiple variable initialization and increment operations, and as a separator in method parameter lists. Syntax Following is the syntax for using comma operator in for loop initialization and increment − for (int i = value1, j = value2; condition; i++, j++) { // loop body } Following is the syntax for using comma as separator in method parameters − MethodName(parameter1, parameter2, parameter3); ...
Read MoreWhat is the base class for all exceptions in C#?
In C#, the base class for all exceptions is System.Exception. This is the root class from which all exception types derive, providing a common structure and functionality for error handling throughout the .NET framework. The System.Exception class has two primary derived classes: System.SystemException for system-generated exceptions and System.ApplicationException for application-specific exceptions. However, Microsoft now recommends deriving custom exceptions directly from System.Exception or its appropriate subclasses rather than from System.ApplicationException. Exception Hierarchy C# Exception Hierarchy System.Exception System.SystemException ...
Read MoreGet all drives in C#
In C#, you can retrieve information about all drives on a system using the DriveInfo.GetDrives() method. This method returns an array of DriveInfo objects representing all logical drives on the current system, including hard drives, network drives, CD-ROMs, and other storage devices. Syntax Following is the syntax for getting all drives − DriveInfo[] drives = DriveInfo.GetDrives(); To iterate through the drives and access their properties − foreach (DriveInfo drive in drives) { Console.WriteLine(drive.Name); } Using GetDrives() to Display Drive Names The simplest approach is to get ...
Read MoreC# Linq SelectMany Method
The SelectMany method in C# LINQ is used to flatten collections of collections into a single sequence. It projects each element of a sequence to an IEnumerable and then flattens the resulting sequences into one sequence. Syntax Following is the basic syntax for SelectMany method − public static IEnumerable SelectMany( this IEnumerable source, Func selector ) Parameters source − The input sequence to transform. selector − A function that transforms each element into a collection. Return Value Returns an IEnumerable whose ...
Read MoreHow is an array declared in C#?
To declare an array in C#, you specify the data type followed by square brackets and the array name. Arrays in C# are reference types that store multiple elements of the same data type in a contiguous memory location. Syntax Following is the basic syntax for declaring an array − datatype[] arrayName; To declare and initialize an array with a specific size − datatype[] arrayName = new datatype[size]; To declare and initialize an array with values − datatype[] arrayName = {value1, value2, value3, ...}; Here − ...
Read MoreTry-Catch-Finally in C#
The try-catch-finally statement in C# provides a structured way to handle exceptions that may occur during program execution. It allows you to gracefully handle errors like division by zero, file access failures, or network timeouts without crashing your application. Exception handling in C# is performed using three main keywords that work together to create a robust error handling mechanism. Syntax Following is the basic syntax of try-catch-finally statement − try { // Code that may throw an exception } catch (ExceptionType ex) { // Handle the exception } finally ...
Read MoreTakeWhile method in C# ()
The TakeWhile() method in C# is a LINQ extension method that returns elements from the beginning of a sequence as long as a specified condition is true. It stops immediately when the first element that doesn't satisfy the condition is encountered, even if later elements might satisfy the condition. Syntax Following is the syntax for the TakeWhile() method − public static IEnumerable TakeWhile( this IEnumerable source, Func predicate ) Parameters source − The sequence to return elements from. predicate − ...
Read MoreC# Queryable SequenceEqual() Method
The SequenceEqual() method in C# is used to determine whether two sequences are equal by comparing elements in the same position. This method returns true if both sequences contain the same elements in the same order, otherwise it returns false. The method is part of the System.Linq namespace and can be used with IQueryable collections to perform element-by-element comparison. Syntax Following is the syntax for the SequenceEqual() method − public static bool SequenceEqual( this IQueryable source1, IEnumerable source2 ) With custom comparer − ...
Read MoreHow to use the ToString() method of array in C#?
The ToString() method in C# returns a string representation of an object. For arrays, the default ToString() method returns the type name rather than the array contents. However, ToString() is commonly used with array properties and methods that return numeric values. Syntax Following is the syntax for using ToString() with array methods − arrayObject.Method().ToString() For converting array elements to strings − array[index].ToString() Using ToString() with Array Properties The ToString() method is useful when converting array properties like bounds and length to string format for display purposes − ...
Read MoreWhy is the Main() method use in C# static?
The Main() method in C# is declared as static because it serves as the entry point of the program and must be callable without creating an instance of the class. When a C# program starts, no objects exist yet, so the runtime needs a way to begin execution without instantiation. Why Main() Must Be Static The static keyword allows the method to be called directly on the class rather than on an instance. Since the program hasn't created any objects when it starts, the Main() method must be accessible without instantiation − Program ...
Read More