Programming Articles

Page 760 of 2547

How to implement Single Responsibility Principle using C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 421 Views

The Single Responsibility Principle (SRP) is the first principle of SOLID design principles in object-oriented programming. It states that a class should have only one reason to change, meaning each class should handle a single responsibility or concern. In this context, responsibility is considered to be one reason to change. When a class has multiple responsibilities, changes to one responsibility can affect the other functionality, making the code harder to maintain and test. Definition The Single Responsibility Principle states that if we have two reasons to change a class, we should split the functionality into two separate ...

Read More

Queue.Count Property in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 235 Views

The Queue.Count property in C# is a read-only property that returns the number of elements currently stored in the Queue collection. This property provides an efficient way to check the size of the queue without needing to iterate through all elements. Syntax The syntax for the Queue.Count property is as follows − public virtual int Count { get; } Return Value The Count property returns an int value representing the total number of elements in the Queue. If the queue is empty, it returns 0. Using Count with Queue Operations The ...

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 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
Nizamuddin Siddiqui
Updated on 17-Mar-2026 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
Siva Sai
Updated on 17-Mar-2026 458 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
AmitDiwan
Updated on 17-Mar-2026 919 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
AmitDiwan
Updated on 17-Mar-2026 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
Nizamuddin Siddiqui
Updated on 17-Mar-2026 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
Siva Sai
Updated on 17-Mar-2026 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
Nizamuddin Siddiqui
Updated on 17-Mar-2026 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
Showing 7591–7600 of 25,466 articles
« Prev 1 758 759 760 761 762 2547 Next »
Advertisements