ref重點
1.傳遞至 ref 參數的引數,在傳遞之前必須先初始化。
2.不能將ref和out用於async修飾詞定義的非同步方法。
3.有進有出
out關鍵字
基本上跟ref一樣
想要多個回傳值的時候,可以使用out
ref跟out在使用上有很微小的差距
使用out的話,他不需要在被調用前初始化
但是調用者需要在返回之前指定輸出的參數
另一方面,可以用這樣去想
out類似於將方法附加返回值,也就是回傳多個值啦
多thread共用變數時,可以用內建的 Interlocked.Increment、Interlocked.Decrement
來Lock變數
static void SumTo10000V3()
{
for (int i = 1; i <= 10000; i++)
{
Interlocked.Increment(ref Total3);
}
}
static void SumTo10000V4()
{
for (int i = 1; i <= 10000; i++)
{
lock (_lockV4)
{
Total4++;
}
}
}
public static void SumTo10000V5()
{
for (int i = 1; i <= 10000; i++)
{
// Acquires the exclusive lock
Monitor.Enter(_lockV5);
try
{
Total5++;
}
finally
{
// Releases the exclusive lock
Monitor.Exit(_lockV5);
}
}
}
static int Total6 = 0;
static object _lockV6 = new object();
public static void SumTo10000V6()
{
for (int i = 1; i <= 10000; i++)
{
bool lockTaken = false;
//1.
//Monitor.Enter(Object obj, Boolean lockTaken)
//1.1.
//Acquires an exclusive lock on the specified object,
//and atomically sets a value that
//indicates whether the lock was taken.
//1.2.
//Boolean lockTaken
//The result of the attempt to acquire the lock, passed by reference.
//The input must be false.
//The output is true if the lock is acquired
Monitor.Enter(_lockV6, ref lockTaken);
try
{
Total6++;
}
finally
{
// Releases the exclusive lock
if (lockTaken)
Monitor.Exit(_lockV6);
}
}
}
2020年5月29日 星期五
訂閱:
張貼留言 (Atom)
WPF聊天室应用(ASP.NET Core SignalR)
WPF聊天室应用(ASP.NET Core SignalR) https://www.bilibili.com/video/BV1Q741187Si?p=2 https://www.bilibili.com/video/BV1UV411e75T?from=search&...
-
介面,依賴反轉,單元測試 https://www.youtube.com/watch?v=GpYieGx4y6M 介面隔離,反射,特性,依賴注入 https://www.youtube.com/watch?v=3bZ4rNS_o10
-
https://mgleon08.github.io/blog/2018/07/16/jwt/ https://medium.com/%E9%BA%A5%E5%85%8B%E7%9A%84%E5%8D%8A%E8%B7%AF%E5%87%BA%E5%AE%B6%E7%AD%86%...
-
https://dotblogs.com.tw/v6610688/2015/02/19/iis_office_access_word_excel_com_interop_api_configuration https://dotblogs.com.tw/gelis/archi...
沒有留言:
張貼留言