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 on Trending Technologies
Technical articles with clear explanations and examples
How can we call one constructor from another in the same class in C#?
In C#, you can call one constructor from another constructor within the same class using the this keyword. This technique is called constructor chaining. To call a constructor from a parent class, use the base keyword. Syntax Following is the syntax for calling another constructor in the same class using this − public ClassName(parameters) : this(arguments) { // additional initialization code } Following is the syntax for calling a parent class constructor using base − public DerivedClass(parameters) : base(arguments) { // derived class initialization code } ...
Read MoreHow can I limit Parallel.ForEach in C#?
The Parallel.ForEach loop in C# executes iterations across multiple threads for improved performance. However, sometimes you need to limit the degree of parallelism to control resource usage, avoid overwhelming external systems, or manage thread contention. This is accomplished using ParallelOptions. Syntax Following is the basic syntax for Parallel.ForEach − Parallel.ForEach(collection, item => { // process item }); Following is the syntax for limiting parallelism using ParallelOptions − Parallel.ForEach(collection, new ParallelOptions { MaxDegreeOfParallelism = maxThreads }, item => { ...
Read MoreWhy singleton class is always sealed in C#?
A singleton class is marked as sealed in C# to prevent inheritance and maintain the single instance guarantee that is fundamental to the singleton pattern. The sealed keyword ensures that no other class can inherit from the singleton class, which could potentially create multiple instances and violate the singleton principle. Why Singleton Classes Must Be Sealed The singleton pattern ensures only one instance of a class exists throughout the application lifecycle. If a singleton class allows inheritance, derived classes could create their own instances, breaking this fundamental rule. Here are the key reasons − Prevents Multiple ...
Read MoreHow to change the Output Encoding Scheme of the C# Console?
The Console.OutputEncoding property in C# allows you to change the character encoding scheme used for console output. This is particularly useful when working with non-ASCII characters, special symbols, or when you need to ensure compatibility with specific encoding standards. By default, the console uses the system's default encoding, but you can override this behavior using different encoding schemes like ASCII, UTF-8, UTF-16, or Unicode. Syntax Following is the syntax for setting the output encoding − Console.OutputEncoding = Encoding.EncodingType; To get the current output encoding − Encoding currentEncoding = Console.OutputEncoding; ...
Read MoreRemoving the specified element from the List in C#
The List.Remove() method in C# removes the first occurrence of a specified element from the list. It returns true if the element was successfully removed, or false if the element was not found in the list. Syntax Following is the syntax for the Remove() method − public bool Remove(T item) Parameters item − The element to remove from the list Return Value Returns true if the element is successfully found and removed; otherwise, false. Using Remove() with String Lists The following example demonstrates removing a string element ...
Read MoreGet the TypeCode for value type Int32 in C#
The TypeCode enumeration in C# represents the type of an object. For Int32 values, the GetTypeCode() method returns TypeCode.Int32, which identifies the value as a 32-bit signed integer. The GetTypeCode() method is inherited from the Object class and implemented by value types to return their specific type identifier. Syntax Following is the syntax for getting the TypeCode of an Int32 value − TypeCode typeCode = intValue.GetTypeCode(); Return Value The GetTypeCode() method returns TypeCode.Int32 for all int variables, regardless of their actual numeric value. Using GetTypeCode() with Int32 Values Example ...
Read MoreHow to Convert Hashtable into an Array?
The Hashtable collection in C# is a non-generic collection of key-value pairs where each key is unique and non-null, while values can be duplicated or null. Converting a Hashtable into an array is a common operation when you need to work with the data in array format. The Hashtable class provides the CopyTo method to efficiently transfer its elements to an array. This method copies Hashtable items as DictionaryEntry objects to a one-dimensional array. Syntax Following is the syntax for the CopyTo method − public virtual void CopyTo(Array array, int arrayIndex); Parameters ...
Read MoreHow to prove that only one instance of the object is created for static class?
In C#, a static class ensures that only one instance exists throughout the application's lifetime. To prove this concept, we can demonstrate how static variables maintain their state across multiple accesses, and how the static constructor is called only once. A static class cannot be instantiated using the new keyword. Instead, all members belong to the type itself rather than to any specific instance. This behavior proves that only one "instance" of the static class exists in memory. Syntax Following is the syntax for declaring a static class with static members − static class ClassName ...
Read MoreHow to resize an Image C#?
Image resizing in C# is a common requirement for optimizing storage space, improving loading times, and adjusting images for different display contexts. The System.Drawing namespace provides the Bitmap class and related classes to handle image manipulation tasks including resizing, compression, and format conversion. A bitmap consists of pixel data for a graphics image and its attributes. GDI+ supports multiple file formats including BMP, GIF, EXIF, JPG, PNG, and TIFF. You can create images from files, streams, and other sources using Bitmap constructors and save them using the Save method. Basic Image Resizing The most straightforward approach to ...
Read MoreMaximum Subarray Sum - Kadane Algorithm using C#
The Maximum Subarray Sum problem involves finding the contiguous subarray within a given array of integers that has the largest sum. Kadane's Algorithm is the most efficient approach to solve this problem in linear time. Given an array that may contain both positive and negative integers, we need to find the maximum sum of any contiguous subarray. Here is an example − Input: arr = [1, -2, 3, 4, -1, 2, 1, -5, 4] Maximum Sum Subarray: [3, 4, -1, 2, 1] Sum = 3 + 4 + (-1) + 2 + 1 = 9 ...
Read More