Skip to content

Instantly share code, notes, and snippets.

@robinli
Last active December 16, 2015 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinli/5361740 to your computer and use it in GitHub Desktop.
Save robinli/5361740 to your computer and use it in GitHub Desktop.
使用 lambda expression pass parameter 時,注意變數是 "參考指標" 時,可能會與預期不同
class Class0
{
public void CallByValue(int si)
{
si *= 2;
}
public void CallByReference(Class0_Parm parm)
{
parm.Si *= 2;
}
public void CallByReference(ref int si)
{
si *= 2;
}
}
class Class0_Parm
{
public int Si { get; set; }
}
static void Main(string[] args)
{// 參數為數值型別 是 Call By Value
int x = 5;
Class0 c0 = new Class0();
Console.Write(string.Format("數值 {0} Call by Value", x));
c0.CallByValue(x);
Console.WriteLine(string.Format(" = {0}", x)); //
// 參數為物件型別 是 Call By Reference
Class0_Parm p = new Class0_Parm() { Si = 5 };
Console.Write(string.Format("物件 {0} Call By Reference", p.Si));
c0.CallByReference(p);
Console.WriteLine(string.Format(" = {0}", p.Si));
int y = 5;
Console.Write(string.Format("數值 {0} Call By Reference(使用ref)", y));
c0.CallByReference(ref y);
Console.WriteLine(string.Format(" = {0}", y));
Console.Read();
}
public void Test(int numSites)
{
System.Timers.Timer[] scanLiveTimer = new System.Timers.Timer[numSites];
for (int si = 0; si < numSites; si++)
{
scanLiveTimer[si] = new System.Timers.Timer();
scanLiveTimer[si].Elapsed +=
new System.Timers.ElapsedEventHandler((sender, e) => ScanMarketEvent(si));
scanLiveTimer[si].Interval = 500;
scanLiveTimer[si].Start();
Console.WriteLine(string.Format("Trigger: {0}", si));
}
}
void ScanMarketEvent(int si)
{
// 有誤,讀到的值都一樣
Console.Write(string.Format("{0} ", si));
}
public void Test(int numSites)
{
MyTimer[] scanLiveTimer = new MyTimer[numSites];
for (int si = 0; si < numSites; si++)
{
scanLiveTimer[si] = new MyTimer();
scanLiveTimer[si].Si = si;
scanLiveTimer[si].Elapsed += new System.Timers.ElapsedEventHandler(MyTimer_Elapsed);
scanLiveTimer[si].Interval = 500;
scanLiveTimer[si].Start();
Console.WriteLine(string.Format("Trigger: {0}", si));
}
}
public class MyTimer : System.Timers.Timer
{
public int Si { get; set; }
}
void MyTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
MyTimer myTimer = (MyTimer)sender;
int si = myTimer.Si;
ScanMarketEvent(si);
}
void ScanMarketEvent(int si)
{
// 可以讀到正確的值
Console.Write(string.Format("{0} ", si));
}
public void Test(int numSites)
{
System.Timers.Timer[] scanLiveTimer = new System.Timers.Timer[numSites];
for (int si = 0; si < numSites; si++)
{
scanLiveTimer[si] = new System.Timers.Timer();
// 複製變數,再傳入 lambda expression
int x = si;
scanLiveTimer[si].Elapsed +=
new System.Timers.ElapsedEventHandler((sender, e) => ScanMarketEvent(x));
scanLiveTimer[si].Interval = 500;
scanLiveTimer[si].Start();
Console.WriteLine(string.Format("Trigger: {0}", si));
}
}
void ScanMarketEvent(int si)
{
// 可以讀到正確的值
Console.Write(string.Format("{0} ", si));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment