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 Nizamuddin Siddiqui
Page 11 of 196
How to call a static constructor or when static constructor is called in C#?
A static constructor in C# is called automatically by the Common Language Runtime (CLR) before the first instance of a class is created or any static members are referenced. It is used to initialize static data or perform actions that need to be executed only once during the application's lifetime. Unlike instance constructors, static constructors cannot be called directly and have no control over when they execute − the CLR handles their invocation automatically. Syntax Following is the syntax for declaring a static constructor − static ClassName() { // initialization code } ...
Read MoreHow to determine if C# .NET Core is installed?
Determining if C# .NET Core is installed on your system is essential for development and deployment. The dotnet command-line interface (CLI) provides several built-in options to check installation status, versions, and available components. These commands will display environment information if .NET Core is installed, or throw an error if it's not found. Using dotnet --info The --info option prints detailed information about the .NET Core installation and machine environment, including the current operating system and commit SHA of the .NET Core version − dotnet --info This command provides comprehensive details about your .NET installation, ...
Read MoreHow 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 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 MoreWhat is the difference between Select and SelectMany in Linq C#?
The Select and SelectMany operators in LINQ C# serve different purposes for data projection. Select produces one result value for every source element, while SelectMany belongs to Projection Operators category and is used to project each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence. Syntax Following is the syntax for Select operator − IEnumerable Select( this IEnumerable source, Func selector ) Following is the syntax for SelectMany operator − IEnumerable SelectMany( this IEnumerable source, ...
Read MoreHow to get a path to the desktop for current user in C#?
The desktop path of the current user can be fetched using Environment.GetFolderPath() with the Environment.SpecialFolder.Desktop enumeration. This method provides a reliable, cross-platform way to access special system folders. Syntax Following is the syntax for getting the desktop path − string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); The Environment.GetFolderPath() method takes an Environment.SpecialFolder enumeration value and returns the path to that special folder as a string. Parameters The method accepts the following parameter − folder − An Environment.SpecialFolder enumeration value that identifies the special folder whose path is to be retrieved. Return ...
Read MoreHow many destructors can we have in one class in C#?
In C#, a destructor is a special method used to clean up resources when an object is destroyed. The answer to how many destructors a class can have is simple: only one destructor per class. Destructors are also called finalizers because they are automatically converted to Finalize() method calls by the compiler. Syntax Following is the syntax for declaring a destructor − class ClassName { ~ClassName() { // cleanup code } } Properties of Destructors Destructors do not ...
Read More