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 18 of 196
How to sort a list of complex types using Comparison delegate in C#?
Sorting a list of complex types in C# can be achieved using the Comparison delegate with the Sort() method. The List.Sort() method has an overload that accepts a Comparison delegate, allowing you to define custom sorting logic for complex objects. The Comparison delegate represents a method that compares two objects of the same type and returns an integer indicating their relative order. Syntax Following is the syntax for the Sort() method using a Comparison delegate − public void Sort(Comparison comparison) The comparison delegate signature is − public delegate int Comparison(T x, ...
Read MoreWhat is the difference between Monitor and Lock in C#?
Both Monitor and lock provide thread synchronization mechanisms in C#, but they serve different purposes. The lock statement is a simplified syntax that internally uses Monitor.Enter and Monitor.Exit with proper exception handling. Monitor offers more advanced features for complex threading scenarios. Syntax Following is the syntax for using lock statement − lock (lockObject) { // critical section code } Following is the syntax for using Monitor class − Monitor.Enter(lockObject); try { // critical section code } finally { Monitor.Exit(lockObject); } ...
Read MoreHow to write retry logic in C#?
Retry logic is implemented to handle transient failures that may resolve themselves after a brief delay. This pattern is essential when working with network operations, database connections, or external APIs that may temporarily fail due to network issues, service overload, or temporary unavailability. It's important to log all connectivity failures that cause a retry so that underlying problems with the application, services, or resources can be identified. Implement retry logic only where you have the full context of a failing operation and when the operation is idempotent (safe to repeat). Syntax Following is the basic syntax for ...
Read MoreWhat is the difference between IEnumerable and IQueryable in C#?
The IEnumerable and IQueryable interfaces are both used for querying collections in C#, but they differ significantly in how they execute queries and handle data. Understanding these differences is crucial for writing efficient database queries and managing memory usage. Key Differences Feature IEnumerable IQueryable Namespace System.Collections System.Linq Query Execution In-memory (client-side) Server-side (database) Lazy Loading Not supported Supported Extension Methods Use functional objects Use expression trees Best For In-memory collections Database queries Syntax Following is the syntax for ...
Read MoreHow to implement interface in anonymous class in C#?
No, anonymous types cannot implement an interface. We need to create your own type. Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler. Syntax Following is the syntax for creating anonymous types − var anonymousObject = new { Property1 = value1, Property2 = value2 }; Why Anonymous Types Cannot Implement ...
Read MoreWhat is bin and obj folder in C#?
Whenever we write a C# code and build or run the solution, it generates 2 folders − bin folder obj folder These folders contain compiled code at different stages of the compilation process. Understanding the difference between them is crucial for C# developers working with build systems and deployment. Why Two Folders? The compilation process in C# goes through multiple stages, which is why we have separate folders for different outputs − Compiling − Individual source files are compiled into intermediate objects Linking − All compiled objects are linked together into the ...
Read MoreWhat is difference between using if/else and switch-case in C#?
The switch statement and if-else statements are both selection statements in C# used for decision-making, but they serve different purposes and have distinct performance characteristics. The switch statement chooses a single section to execute from a list of candidates based on pattern matching, while if-else provides conditional branching based on boolean expressions. Syntax Following is the syntax for a switch statement − switch (expression) { case value1: // code block break; case ...
Read MoreWhat is #if DEBUG and How to use it in C#?
In Visual Studio, Debug mode and Release mode are different configurations for building your .NET project. The #if DEBUG directive is a preprocessor conditional compilation directive that allows you to include or exclude code blocks based on whether the DEBUG symbol is defined. The Debug mode does not optimize the binary it produces because the relationship between source code and generated instructions is more complex. This allows breakpoints to be set accurately and allows programmers to step through the code one line at a time. The Debug configuration compiles with full symbolic debug information, while the Release configuration has ...
Read MoreWhat are the improvements in Out Parameter in C# 7.0?
C# 7.0 introduced significant improvements to out parameters that make code more concise and readable. The major enhancement is the ability to declare out variables inline as arguments to the method where they're used, eliminating the need for separate variable declarations. Syntax Prior to C# 7.0, out variables had to be declared before use − int result; if (int.TryParse("123", out result)) { // use result } C# 7.0 allows inline declaration − if (int.TryParse("123", out int result)) { // use result immediately } Key ...
Read MoreHow to subscribe to an Event in C# and can we have multiple subscribers to one Event in a C#?
Events enable a class or object to notify other classes or objects when something of interest occurs. The class that raises the event is called the publisher and the classes that handle the event are called subscribers. An event can have multiple subscribers, and a subscriber can handle multiple events from multiple publishers. Events that have no subscribers are never raised. The publisher determines when an event is raised; the subscribers determine what action is taken in response to the event. Syntax Following is the syntax for declaring an event using EventHandler − public event ...
Read More