ThreadStart with parameters
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Understanding ThreadStart with Parameters in C#
Introduction
The concept of threading in software development allows for the execution of multiple operations concurrently, thereby optimizing performance and responsiveness. In C#, the Thread class is a fundamental structure used for creating and managing threads. This article delves into the ThreadStart delegate in C#, with a focus on handling methods that require parameters.
The ThreadStart Delegate
In C#, the ThreadStart delegate comes into play when initializing a new thread to execute a certain method. The basic form of using ThreadStart is when the method to be executed doesn’t take any parameters, represented as:
Here, SomeMethod is a method that takes no parameters.
The Need for Parameters
Often, the task you want to execute on a separate thread requires input data. However, ThreadStart does not natively support methods with parameters. To address this, C# provides the ParameterizedThreadStart delegate.
Using ParameterizedThreadStart
The ParameterizedThreadStart delegate allows us to pass a single object to the method that will be executed on a new thread. Here's an example:
In this example, PrintMessage accepts an object type parameter, allowing us to pass any data type to it. This approach utilizes boxing to allow greater flexibility at the cost of type safety.
Limitations of ParameterizedThreadStart
The primary limitation of using ParameterizedThreadStart is that it only accepts a single object parameter. For methods requiring multiple arguments, developers typically use one of the following approaches:
- Tuple or Custom Class: Wrap multiple parameters within a
Tupleor define a custom class to group the parameters into a single object.
Example with Tuple
Best Practices
- Type Safety: When dealing with multiple parameters or complex parameter structures, consider creating a well-defined class. This not only promotes type safety but also improves code readability.
- Avoiding Boxing: If performance is critical, try to avoid boxing and unboxing by using more explicit thread handling strategies or newer techniques such as
Taskand async/await patterns.
Summary
The following table summarizes key points about using ThreadStart and ParameterizedThreadStart:
| Aspect | ThreadStart | ParameterizedThreadStart |
| Parameter Support | No | Yes, but limited to a single object |
| Type Safety | High | Lower due to boxing/unboxing |
| Versatility | Low (methods without parameters only) | Higher (any data type through object, but needs casting) |
| Use Cases | Simple, parameter-less methods Background tasks | Methods requiring input data Complex thread operations |
| Alternative Solutions | Lambda expressions Anonymous methods | Custom classes Tuples |
Conclusion
Understanding the capabilities and limitations of ThreadStart and ParameterizedThreadStart is crucial for effective multithreading in C#. While they provide foundational support for threaded operations, modern C# offers more powerful constructs, such as Tasks, async, and await, which are typically preferred for their enhanced capabilities in handling asynchronous processes. However, when direct control over thread parameters is necessary, knowing how to efficiently use these delegates remains an essential skill for developers.
Related reading

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.