C# Program to pass Parameter to a Thread


To work with threads, add the following namespace in your code −

using System.Threading;

Firstly, you need to create a new thread in C# −

Thread thread = new Thread(threadDemo);

Above, threadDemo is our thread function.

Now pass a parameter to the thread −

thread.Start(str);

The parameter set above is −

String str = "Hello World!";

Example

Let us see the complete code to pass a parameter to a thread in C#.

Live Demo

using System;
using System.Threading;
namespace Sample {
   class Demo {
      static void Main(string[] args) {
         String str = "Hello World!";
         // new thread
         Thread thread = new Thread(threadDemo);
         // passing parameter
         thread.Start(str);
      }
      static void threadDemo(object str) {
         Console.WriteLine("Value passed to the thread: "+str);
      }
   }
}

Output

Value passed to the thread: Hello World!

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 19-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements