How do I copy items from list to list without foreach in C#?

Nizamuddin Siddiqui
Updated on 17-Mar-2026 07:04:36

1K+ Views

There are several efficient methods to copy items from one list to another in C# without using a foreach loop. These methods provide different approaches depending on whether you need a complete copy, partial copy, or want to append items to an existing list. Using List Constructor The most straightforward method is to use the List constructor that accepts an IEnumerable parameter. This creates a new list with all elements from the source list − using System; using System.Collections.Generic; class Program { public static void Main() { ... Read More

How to implement Open Closed principle using C#?

Nizamuddin Siddiqui
Updated on 17-Mar-2026 07:04:36

593 Views

The Open Closed Principle (OCP) is one of the five SOLID principles of object-oriented programming. It states that software entities like classes, modules and functions should be open for extension but closed for modifications. Definition The Open Closed Principle states that the design and writing of code should be done in a way that new functionality can be added with minimum changes to existing code. The design should allow adding new functionality through new classes while keeping existing code unchanged as much as possible. Open Closed Principle OPEN ... Read More

Creating an Index From the Specified Index at the Start of a Collection in C#

Siva Sai
Updated on 17-Mar-2026 07:04:36

464 Views

In C#, manipulating collections is a frequent operation, with indexing being a crucial part of this process. The Index struct, introduced in C# 8.0, provides a powerful way to create indices that can represent positions from the start or end of a collection. This article will guide you through creating and using indices from specified positions at the start of collections in C#. Syntax Following is the syntax for creating an Index from the start of a collection − Index index = new Index(value, fromEnd: false); // OR using implicit conversion Index index = value; ... Read More

Queue.Dequeue Method in C#

AmitDiwan
Updated on 17-Mar-2026 07:04:36

921 Views

The Queue.Dequeue() method in C# is used to remove and return the object at the beginning of the Queue. This method follows the FIFO (First In, First Out) principle, where the first element added to the queue is the first one to be removed. Syntax Following is the syntax for the Dequeue() method − public T Dequeue(); Return Value The method returns the object that is removed from the beginning of the Queue. If the queue is empty, it throws an InvalidOperationException. Queue.Dequeue() Operation ... Read More

Remove all the strings from the StringCollection in C#

AmitDiwan
Updated on 17-Mar-2026 07:04:36

182 Views

The StringCollection class in C# provides the Clear() method to remove all strings from the collection at once. This method is part of the System.Collections.Specialized namespace and is useful when you need to empty the entire collection efficiently. Syntax Following is the syntax for the Clear() method − stringCollection.Clear(); Parameters The Clear() method does not take any parameters. Return Value The Clear() method does not return any value. It has a return type of void. StringCollection.Clear() Process Before Clear() ... Read More

How to calculate the total number of items defined in an enum in C#?

Nizamuddin Siddiqui
Updated on 17-Mar-2026 07:04:36

2K+ Views

An enum is a special value type that represents a group of named constants. When working with enums, you often need to know how many items are defined in the enum. C# provides several methods to calculate the total count of enum items. Syntax Following are the main approaches to get the total count of enum items − // Using Enum.GetNames() int count = Enum.GetNames(typeof(EnumName)).Length; // Using Enum.GetValues() int count = Enum.GetValues(typeof(EnumName)).Length; Using Enum.GetNames() Method The Enum.GetNames() method returns an array of string names for all enum values. The Length property gives ... Read More

Cross Join in LINQ

Siva Sai
Updated on 17-Mar-2026 07:04:36

1K+ Views

Language Integrated Query (LINQ) is a powerful tool in C# for data manipulation, allowing for efficient and expressive data access and manipulation. One of the operations you can perform with LINQ is the cross join operation, which creates a Cartesian product between two data sources. Understanding Cross Join Cross join, also known as Cartesian product, is a type of join operation that matches each row of the first table with every row of the second table. If the first table has n rows and the second table has m rows, the result is a table with n×m rows. ... Read More

How to find the Number of CPU Cores in C#?

Nizamuddin Siddiqui
Updated on 17-Mar-2026 07:04:36

3K+ Views

Finding the number of CPU cores in C# involves understanding different types of processor information. There are several ways to get processor details − Physical processors − The actual number of CPU sockets CPU cores − The number of physical processing units Logical processors − The number of threads the CPU can handle simultaneously These values can differ significantly. For example, a machine with 2 dual-core hyper-threading-enabled processors has 2 physical processors, 4 cores, and 8 logical processors. CPU Architecture Example Physical CPU 1 ... Read More

How to set the alignment of Check Mark in CheckBox in C#?

Jaisshree
Updated on 17-Mar-2026 07:04:36

676 Views

In a graphical user interface, a CheckBox control in C# enables users to pick a true/false or yes/no choice. The check mark in a CheckBox control is normally aligned to the left, but you can customize its position using the CheckAlign property. Syntax The syntax to change the alignment of the check mark is − checkBox1.CheckAlign = ContentAlignment.MiddleRight; The CheckAlign property accepts values from the ContentAlignment enumeration − MiddleLeft − Align the check mark to the left edge of the control (default). MiddleRight − Align the check mark to ... Read More

Default Interface Methods in C#

Siva Sai
Updated on 17-Mar-2026 07:04:36

696 Views

Default interface methods are a game-changing feature introduced in C# 8.0 that allow developers to add new methods to an interface without breaking existing implementations. This feature enables interface evolution while maintaining backward compatibility with existing code. Syntax Following is the syntax for defining a default interface method − public interface IInterfaceName { void MethodName() { // default implementation } } A class can use the default implementation or override it − public class ClassName : IInterfaceName { ... Read More

Advertisements