Programming Articles

Page 52 of 2547

What is the difference between Foreach and Parallel.Foreach in C#?

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

The foreach loop in C# runs on a single thread and processes items sequentially one by one. In contrast, Parallel.ForEach utilizes multiple threads to process items concurrently, potentially improving performance for CPU-intensive operations on large collections. The key difference is that Parallel.ForEach can distribute work across multiple threads, while the standard foreach executes on a single thread. To use Parallel.ForEach, you need to import the System.Threading.Tasks namespace. Syntax Following is the syntax for a standard foreach loop − foreach (var item in collection) { // sequential processing } Following is ...

Read More

What is the difference between Task.WhenAll() and Task.WaitAll() in C#?

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

The Task.WaitAll() and Task.WhenAll() methods in C# serve different purposes when working with multiple asynchronous tasks. Task.WaitAll() blocks the current thread until all tasks complete, while Task.WhenAll() returns a new task that represents the completion of all provided tasks without blocking the calling thread. Understanding the difference between these methods is crucial for building responsive applications, especially those with user interfaces where blocking the main thread can freeze the UI. Syntax Following is the syntax for Task.WaitAll() − Task.WaitAll(task1, task2, task3); // or with timeout Task.WaitAll(new Task[] { task1, task2 }, TimeSpan.FromSeconds(30)); Following ...

Read More

Random.Next() Method in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 3K+ Views

The Random.Next() method in C# is used to generate non-negative random integers. It provides multiple overloads to control the range of generated numbers, making it useful for various scenarios like games, simulations, and testing. Syntax The Random.Next() method has three overloads − public virtual int Next(); public virtual int Next(int maxValue); public virtual int Next(int minValue, int maxValue); Parameters maxValue − The exclusive upper bound of the random number (0 to maxValue-1). minValue − The inclusive lower bound of the random number. maxValue − The exclusive upper bound of the random number ...

Read More

Check if ListDictionary has a fixed size in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 246 Views

The ListDictionary class in C# provides the IsFixedSize property to determine whether the dictionary has a fixed size. A ListDictionary is a lightweight collection that stores key-value pairs using a singly linked list implementation, making it ideal for small collections (typically 10 or fewer items). Syntax Following is the syntax for checking if a ListDictionary has a fixed size − bool isFixed = listDictionary.IsFixedSize; Return Value The IsFixedSize property returns a bool value − true − The ListDictionary has a fixed size and cannot grow or shrink false ...

Read More

Uri.HexEscape(Char) Method in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 199 Views

The Uri.HexEscape() method in C# converts a specified character into its hexadecimal equivalent representation using percent-encoding. This method is particularly useful for URL encoding where certain characters need to be represented in hexadecimal format for safe transmission in URIs. Syntax Following is the syntax − public static string HexEscape(char ch); Parameters ch − The character to convert to hexadecimal representation. Return Value Returns a string containing the hexadecimal representation of the character in the format %XX, where XX is the two-digit hexadecimal value. HexEscape Character Conversion ...

Read More

How to update the value stored in a Dictionary in C#?

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

In C#, Dictionary is a generic collection which is generally used to store key/value pairs. In Dictionary, the key cannot be null, but value can be. A key must be unique. Duplicate keys are not allowed if we try to use duplicate key then compiler will throw an exception. As mentioned above a value in a dictionary can be updated by using its key as the key is unique for every value. Syntax Following is the syntax for updating a dictionary value − myDictionary[myKey] = myNewValue; You can also use the TryGetValue method ...

Read More

What is the difference between FromBody and FromUri attributes in C# ASP.NETnWebAPI?

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

In ASP.NET Web API, parameter binding determines how action method parameters receive values from HTTP requests. The [FromUri] and [FromBody] attributes control the source of parameter data, providing explicit control over this binding process. The FromUri attribute forces Web API to bind parameters from the URI query string, route data, or headers instead of the request body. The FromBody attribute instructs Web API to read parameter values from the HTTP request body using formatters like JSON or XML. Syntax Following is the syntax for using [FromUri] attribute − public IActionResult Method([FromUri] ModelClass model) { ...

Read More

Check if StringDictionary is synchronized in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 167 Views

The StringDictionary class in C# provides the IsSynchronized property to check whether the dictionary is synchronized (thread-safe). By default, StringDictionary is not synchronized, meaning it's not safe for concurrent access from multiple threads without external synchronization. Syntax Following is the syntax to check if a StringDictionary is synchronized − bool isSynchronized = stringDictionary.IsSynchronized; Properties IsSynchronized − Returns true if the StringDictionary is synchronized; otherwise, false. SyncRoot − Gets an object that can be used to synchronize access to the StringDictionary. Example 1: Basic Synchronization Check using System; using ...

Read More

Get all the interfaces implemented or inherited by the current Type in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 598 Views

The Type class in C# provides methods to retrieve all interfaces implemented or inherited by a specific type. The GetInterfaces() method returns an array of all interfaces, while GetInterface() method retrieves a specific interface by name. Syntax Following is the syntax for getting all interfaces implemented by a type − Type[] interfaces = type.GetInterfaces(); Following is the syntax for getting a specific interface by name − Type specificInterface = type.GetInterface("InterfaceName", ignoreCase); Parameters GetInterface(string name) − Returns the interface with the specified name. GetInterface(string name, bool ignoreCase) − Returns ...

Read More

How to create Guid value in C#?

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

A Globally Unique Identifier or Guid represents a 128-bit identification number that is mathematically guaranteed to be unique across multiple systems and distributed applications. The total number of unique keys (approximately 3.40282366×10³⁸) is so large that the probability of generating the same number twice is negligible. GUIDs are commonly used in applications where unique identification is critical, such as database primary keys, transaction IDs, or session identifiers. They are typically displayed as a sequence of hexadecimal digits like 3F2504E0-4F89-11D3-9A0C-0305E82C3301. Syntax The Guid structure is present in the System namespace. Following are the most common ways to create ...

Read More
Showing 511–520 of 25,467 articles
« Prev 1 50 51 52 53 54 2547 Next »
Advertisements