Csharp Articles

Page 67 of 196

How to convert IEnumerable to List and List back to IEnumerable in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 9K+ Views

IEnumerable is an interface that defines a single method GetEnumerator() and provides the foundation for iterating over collections. The List class is a concrete implementation that provides indexed access and manipulation methods for collections. Converting between these types is a common requirement in C# development. IEnumerable offers read-only iteration, while List provides full collection manipulation capabilities including adding, removing, and sorting elements. Converting IEnumerable to List The most efficient way to convert IEnumerable to List is using the ToList() extension method − Using ToList() Method using System; using System.Collections.Generic; using System.Linq; class Program ...

Read More

How to get all the directories and sub directories inside a path in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 26K+ Views

To get directories in C#, the Directory.GetDirectories method is used. This method returns the names of subdirectories (including their full paths) that match a specified search pattern in a given directory, with options to search subdirectories recursively. The method allows you to control the search scope using SearchOption enumeration values. The wildcard pattern * matches zero or more characters, enabling flexible directory matching. Syntax Following is the basic syntax for Directory.GetDirectories method − string[] Directory.GetDirectories(string path, string searchPattern, SearchOption searchOption) Parameters path − The relative or absolute path to the directory ...

Read More

How to catch an exception thrown by an async void method in C#?

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

In synchronous C# code, exceptions propagate up the call stack until they reach an appropriate catch block. However, exception handling in asynchronous methods behaves differently depending on the return type. An async method in C# can have three return types: void, Task, and Task. When an exception occurs in an async method with Task or Task return type, the exception is wrapped in an AggregateException and attached to the Task object. However, async void methods behave differently and present unique challenges for exception handling. Async Task vs Async Void Exception Handling Exception Handling: ...

Read More

How do I get a human-readable file size in bytes abbreviation using C#?

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

To get a human-readable file size in bytes with proper abbreviations, C# requires converting raw byte values into appropriate units like KB, MB, GB, or TB. This involves dividing by 1024 (or 1000 depending on your preference) and selecting the most appropriate unit to display. The key is to create a method that automatically determines the best unit and formats the size accordingly, rather than showing all units simultaneously. Syntax To get file size in bytes − long sizeInBytes = new FileInfo(filePath).Length; To format the size into human-readable format − string ...

Read More

What is connection pooling in C# and how to achieve it?

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

Connection pooling in C# is a technique that improves database application performance by reusing database connections rather than creating new ones for each request. When a connection is closed, it's returned to a pool for future use instead of being destroyed. The .NET Framework automatically manages connection pooling for ADO.NET connections. When you use the using statement with database connections, it ensures proper disposal and automatic participation in connection pooling. How Connection Pooling Works Connection Pool Lifecycle Application Connection Pool ...

Read More

How to get all the files, sub files and their size inside a directory in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

To get all files and subdirectories within a directory in C#, the Directory.GetFiles method provides a comprehensive solution. This method returns the names of all files (including their full paths) that match a specified search pattern and can optionally search through subdirectories. The FileInfo class allows you to retrieve detailed information about each file, including its size, creation date, and other properties. Syntax Following is the syntax for using Directory.GetFiles − string[] files = Directory.GetFiles(path, searchPattern, searchOption); Parameters path − The directory path to search searchPattern − The search pattern (e.g., ...

Read More

Why singleton class is always sealed in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

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 More

What is Liskov Substitution principle and how to implement in C#?

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

The Liskov Substitution Principle (LSP) is one of the five SOLID principles in object-oriented programming. It states that derived types must be completely substitutable for their base types without altering the correctness of the program. In other words, objects of a superclass should be replaceable with objects of its subclasses without breaking the application. Definition According to LSP, we should be able to treat a child class as though it were the parent class. This means that all derived classes should retain the functionality of their parent class and cannot replace any functionality the parent provides in a ...

Read More

What is Interface segregation principle and how to implement it in C#?

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

The Interface Segregation Principle (ISP) is the fourth principle of SOLID design principles. It states that clients should not be forced to depend upon interfaces that they don't use. Instead of creating one large interface with many methods, it's better to create multiple smaller, focused interfaces that serve specific purposes. This principle promotes high cohesion and loose coupling by ensuring that classes only implement the methods they actually need, making the code more maintainable and flexible. Syntax Following is the syntax for creating segregated interfaces − public interface ISpecificInterface { // Only ...

Read More

What is dependency inversion principle and how to implement in C#?

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

The Dependency Inversion Principle (DIP) is one of the five SOLID principles in object-oriented design. It states that high-level modules should not depend on low-level modules. Both should depend on abstractions. Additionally, abstractions should not depend on details — details should depend on abstractions. This principle helps reduce tight coupling between classes, making code more flexible, testable, and maintainable. By depending on abstractions rather than concrete implementations, you can easily swap out dependencies without modifying existing code. Key Rules of Dependency Inversion High-level modules should not depend directly on low-level modules. Both high-level ...

Read More
Showing 661–670 of 1,951 articles
« Prev 1 65 66 67 68 69 196 Next »
Advertisements