Please let me know why ParameterizedThreadStart class only allow method which only System.object argument type contain.
public class MainThreadTest
{
    public static void Main(string[] args)
    {
        Thread T = new Thread(new ParameterizedThreadStart(DisplayYOrX));
        T.Start("X");
        DisplayYOrX("Y");
    }
    static void DisplayYOrX(object outValue)
    {
        string Parameter = (string)outValue;
        for(int i=0; i<10; i++)
            Console.Write(Parameter);
    }
}
Why I would like to know about that is I do not want to use type cast syntax again.
string Parameter = (string)outValue;
                Why is the Constructor of the Thread class Taking a Parameter of Delegate Type? As we already discussed, the main objective of creating a Thread in C# is to execute a function. A delegate is a type-safe function pointer.
The ParameterizedThreadStart delegate supports only a single parameter.
To pass data into the thread we will simply pass it as an input parameter to the method that is being started as a new thread. To get the data from the thread a delegate callback method will be used. First, we will make a regular method that takes in an input parameter.
The reason for the limitation is that ThreadStart isn't a generic delegate and hence it's only capable of passing an object around.  This is easy enough to work around though by using a lambda where you directly pass the value.
public static void Main(string[] args) {
  ThreadStart start = () => { 
    DisplayYOrX("X");
  };
  Thread t = new Thread(start);
  t.Start();
  ...
}
static void DisplayYOrX(string outValue) {
  ...
}
Version for C# 2.0
public static void Main(string[] args) {
  ThreadStart start = delegate { 
    DisplayYOrX("X");
  };
  Thread t = new Thread(start);
  t.Start();
  ...
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With