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
Articles by Ankith Reddy
Page 8 of 73
What are sealed modifiers in C#?
The sealed modifier in C# prevents method overriding in derived classes. When applied to an overridden method, it stops further inheritance of that method. The sealed method must be part of a derived class and must override a virtual or abstract method from its base class. Syntax Following is the syntax for declaring a sealed method − public sealed override ReturnType MethodName() { // method implementation } Key Rules for Sealed Methods A sealed method must be an override of a virtual or abstract method. Once ...
Read MoreC# Linq ThenBy Method
The ThenBy() method in C# LINQ is used to perform a secondary sort on a sequence that has already been ordered using OrderBy(). It allows you to sort by multiple criteria, where OrderBy() defines the primary sort and ThenBy() defines the secondary sort for elements that are equal in the primary sort. Syntax Following is the syntax for using ThenBy() method − IEnumerable result = source.OrderBy(keySelector1).ThenBy(keySelector2); You can chain multiple ThenBy() methods for additional sort levels − IEnumerable result = source.OrderBy(key1).ThenBy(key2).ThenBy(key3); Parameters keySelector − A function that ...
Read MoreRemove Leading Zeros from a String in C#
Removing leading zeros from a string is a common requirement in C# when dealing with numeric data or formatted strings. There are several approaches to accomplish this task, with TrimStart() being the most straightforward method. Syntax Following is the syntax for using TrimStart() to remove leading zeros − string.TrimStart('0') string.TrimStart(new char[] { '0' }) Using TrimStart() Method The TrimStart() method removes specified characters from the beginning of a string. To remove leading zeros, we pass '0' as the character to trim − using System; class Program { ...
Read MoreGet bounds of a C# three-dimensional array
To get the bounds of a three-dimensional array in C#, use the GetUpperBound() and GetLowerBound() methods. These methods return the highest and lowest indices for a specified dimension of the array. The parameter passed to these methods specifies the dimension (0, 1, or 2 for a three-dimensional array). Understanding array bounds is crucial for safe array traversal and avoiding index out-of-range exceptions. Syntax Following is the syntax for getting array bounds − array.GetUpperBound(dimension) array.GetLowerBound(dimension) Parameters dimension: An integer specifying the dimension of the array (0-based indexing). Return ...
Read MoreC# Program to write a number in hexadecimal format
Converting numbers to hexadecimal format in C# can be accomplished using various format specifiers. Hexadecimal is a base-16 number system commonly used in programming for representing binary data in a more readable format. Syntax Following are the main hexadecimal format specifiers in C# − {0:x} // lowercase hexadecimal {0:X} // uppercase hexadecimal {0:x8} // lowercase with 8 digits (zero-padded) {0:X8} // uppercase with 8 digits (zero-padded) You can also use the ToString() method with format specifiers − number.ToString("x") // ...
Read MoreImplicit conversion from Byte to Decimal in C#
Byte represents an 8-bit unsigned integer that can store values from 0 to 255. Implicit conversion from byte to decimal is possible in C# because there is no loss of data − all byte values can be exactly represented as decimal values. Implicit conversion happens automatically without requiring any casting operator or explicit conversion method. Syntax The syntax for implicit conversion from byte to decimal is − byte byteValue = value; decimal decimalValue = byteValue; // Implicit conversion How Implicit Conversion Works When you assign a byte value to a decimal variable, ...
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 MoreMethods of the Thread Class
The Thread class in C# provides several methods to control and manage thread execution. The most commonly used methods are Start(), Sleep(), Join(), and Abort() (though Abort() is obsolete in .NET Core and later versions). Commonly Used Thread Methods Method Description Start() Starts the execution of a thread Sleep(int milliseconds) Pauses the current thread for specified time Join() Blocks calling thread until this thread terminates Interrupt() Interrupts a thread in WaitSleepJoin state Using Start() and Join() Methods The Start() method ...
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 MoreHow to initialize a string to an empty string in C#?
In C#, there are several ways to initialize a string to an empty string. Understanding the different approaches helps you choose the most appropriate method for your specific use case. Syntax Following are the different ways to initialize a string to an empty string − string myStr = ""; // Empty string literal string myStr = string.Empty; // Using string.Empty property string myStr = null; // Null reference (not ...
Read More