亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何能傳入一個引用類型,或者能在函數內部改變的外部變量 , 怎么改?

我如何能傳入一個引用類型,或者能在函數內部改變的外部變量 , 怎么改?

飲歌長嘯 2022-07-15 17:13:58
using System;using System.Threading;public class Work{public static void Main(){// To start a thread using a shared thread procedure, use// the class name and method name when you create the// ParameterizedThreadStart delegate. C# infers the// appropriate delegate creation syntax:// new ParameterizedThreadStart(Work.DoWork)//Thread newThread = new Thread(Work.DoWork);// Use the overload of the Start method that has a// parameter of type Object. You can create an object that// contains several pieces of data, or you can pass any// reference type or value type. The following code passes// the integer value 42.//newThread.Start(42);// To start a thread using an instance method for the thread// procedure, use the instance variable and method name when// you create the ParameterizedThreadStart delegate. C# infers// the appropriate delegate creation syntax:// new ParameterizedThreadStart(w.DoMoreWork)//Work w = new Work();newThread = new Thread(w.DoMoreWork);// Pass an object containing data for the thread.//newThread.Start("The answer.");}public static void DoWork(object data){Console.WriteLine("Static thread procedure. Data='{0}'",data);}public void DoMoreWork(object data){Console.WriteLine("Instance thread procedure. Data='{0}'",data);}}上面是我找來的一個例子thread函數的參數是 object類型
查看完整描述

3 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

應該不能改吧,我一直用一個比較笨的方法。在類中另開一個元素,然后就不用傳參數了。修改的用//----- 加以標注

using System;
using System.Threading;

public class Work
{
public static void Main()
{
// To start a thread using a shared thread procedure, use
// the class name and method name when you create the
// ParameterizedThreadStart delegate. C# infers the
// appropriate delegate creation syntax:
// new ParameterizedThreadStart(Work.DoWork)
//
Thread newThread = new Thread(Work.DoWork);

// Use the overload of the Start method that has a
// parameter of type Object. You can create an object that
// contains several pieces of data, or you can pass any
// reference type or value type. The following code passes
// the integer value 42.
//
newThread.Start(42);

// To start a thread using an instance method for the thread
// procedure, use the instance variable and method name when
// you create the ParameterizedThreadStart delegate. C# infers
// the appropriate delegate creation syntax:
// new ParameterizedThreadStart(w.DoMoreWork)
//
Work w = new Work();
newThread = new Thread(w.DoMoreWork);

// Pass an object containing data for the thread.
//
newThread.Start("The answer.");
}

public static void DoWork(object data)
{
Console.WriteLine("Static thread procedure. Data='{0}'",
data);
}

public int changeInDoMoreWork = 0; //--------
public void DoMoreWork(object data)
{
changeInDoMoreWork=8; //------------
Console.WriteLine("Instance thread procedure. Data='{0}'",
data);
}
}


查看完整回答
反對 回復 2022-07-18
?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

你的表達我不是太理解,thread的參數可以用委托,委托可以捆綁引用類型的。

查看完整回答
反對 回復 2022-07-18
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

參數前面加上 ref 吧

查看完整回答
反對 回復 2022-07-18
  • 3 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號