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 24 of 196
How to implement Single Responsibility Principle using C#?
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 MoreHow do I copy items from list to list without foreach in C#?
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 MoreHow to implement Open Closed principle using C#?
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 MoreHow to calculate the total number of items defined in an enum in C#?
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 MoreHow to find the Number of CPU Cores in C#?
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 MoreHow to get the thread ID from a thread in C#?
A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time-consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job. Threads are lightweight processes. One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application. In C#, the System.Threading.Thread class is used for working with threads. It allows creating and accessing individual threads ...
Read MoreHow to display methods and properties using reflection in C#?
Reflection is the process of examining and describing the metadata of types, methods, and properties in a code at runtime. The System.Reflection namespace enables you to obtain data about loaded assemblies, the elements within them like classes, methods, and value types. The most commonly used classes in System.Reflection include Assembly, AssemblyName, ConstructorInfo, MethodInfo, ParameterInfo, EventInfo, PropertyInfo, and MemberInfo. Syntax Following is the syntax for getting type information using reflection − TypeInfo typeInfo = typeof(ClassName).GetTypeInfo(); IEnumerable properties = typeInfo.DeclaredProperties; IEnumerable methods = typeInfo.DeclaredMethods; Using Reflection to Display Properties and Methods Reflection allows you ...
Read MoreHow to read a CSV file and store the values into an array in C#?
A CSV file is a comma-separated values file that stores data in a tabular format. Most business organizations use CSV files to store and exchange structured data because of their simplicity and wide compatibility. In C#, we can read CSV files using the StreamReader class along with File.OpenRead() method. The data can then be parsed and stored in arrays or collections for further processing. Syntax Following is the basic syntax for reading a CSV file − StreamReader reader = new StreamReader(File.OpenRead(filePath)); while (!reader.EndOfStream) { var line = reader.ReadLine(); ...
Read MoreHow to perform a left outer join using linq extension methods in C#?
In LINQ, a Left Outer Join includes all elements from the left collection and matching elements from the right collection. When there's no match in the right collection, the result still includes the left element with null values for the right side. This is different from an Inner Join, which only includes matching elements from both collections. Left Outer Join ensures that no records from the left collection are lost, even when they don't have corresponding matches in the right collection. Syntax The Left Outer Join pattern using LINQ extension methods follows this structure − ...
Read MoreWhat does LINQ return when the results are empty in C#?
Language-Integrated Query (LINQ) is a set of technologies that integrates query capabilities directly into the C# language. When LINQ queries return no results, the behavior depends on which LINQ method you use. LINQ queries can be written for SQL Server databases, XML documents, ADO.NET Datasets, and any collection that supports IEnumerable or IEnumerable. Understanding what happens when queries return empty results is crucial for avoiding runtime exceptions. Common LINQ Methods and Empty Results LINQ Method Empty Result Behavior ToList() Returns an empty List ToArray() Returns an empty array ...
Read More