Model層的類如下:
public class Order
{
public int Id { get; set; }
public decimal Amount { get; set; }
public string CustomerName { get; set; }
public string Status { get; set; }
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Program.cs代碼如下:
class Program
{
//通俗的來講隻有等autorevent.Set()成功運作後,autorevent.WaitOne()才能夠獲得運作機會;Set是發信号,WaitOne是等待信号,隻有發了信号,等待的才會執行。如果不發的話,WaitOne後面的程式就永遠不會執行。
private static AutoResetEvent autorevent= new AutoResetEvent(false);
private static List<Person> list = new List<Person>()
{
new Person() {Name = "羅斯", Age = 19},
new Person() {Name = "斯蒂夫", Age = 35},
new Person() {Name = "傑西卡", Age = 30},
};
private static void Main(string[] args)
{
//CheckOrders();
//Common();
//RemoveFromList();
//ExceptionHandling();
//-----------------------------------------------------------------
//--------------------------------mock 非線程 safe----------------------------
Thread t1 = new Thread(() =>
{
//確定等待t2開始之後才運作下面的代碼
autorevent.WaitOne();
foreach (var item in list)
{
Console.WriteLine("t1:" + item.Name);
Thread.Sleep(1000);
}
});
t1.Start();
Thread t2 = new Thread(() =>
{
//通知t1可以執行代碼
autorevent.Set();
//沉睡1秒是為了確定删除操作在t1的疊代過程中
Thread.Sleep(1000);
list.RemoveAt(2);
});
t2.Start();
Console.ReadKey();
}
public static void CheckOrders()
{
List<Order> orders = new List<Order>()
{
new Order { Id = 123, Amount = 29.95m, CustomerName = "馬克", Status = "Delivered" },
new Order { Id = 456, Amount = 45.00m, CustomerName = "斯蒂芬", Status = "Refunded" },
new Order { Id = 768, Amount = 32.50m, CustomerName = "克萊爾", Status = "Delivered" },
};
bool anyRefunded = orders.Any(o => o.Status == "Refunded");
if (anyRefunded)
{
Console.WriteLine("有退款訂單");
}
else
{
Console.WriteLine("沒有退款訂單");
}
bool allDelivered = orders.All(o => o.Status == "Delivered");
if (allDelivered)
{
Console.WriteLine("都已發貨");
}
else
{
Console.WriteLine("未全部都已發貨");
}
}
public static void Common()
{
var daysToChristmas = (new DateTime(DateTime.Today.Year, 12, 25) - DateTime.Today).TotalDays;
Console.WriteLine(daysToChristmas);
//-----------------------------------------------------------------
int SUM = "10,5,0,8,10,1,4,0,10,1"
.Split(',')
.Select(int.Parse)
.OrderBy(n => n)
.Skip(3)
.Sum();
Console.WriteLine(SUM);
//-----------------------------------------------------------------
var customers = new[] {
new { Name = "安妮", Email = "" },
new { Name = "本", Email = "" },
new { Name = "莉莉", Email = "" },
new { Name = "嬌兒", Email = "" },
new { Name = "桑木", Email = "" },
};
foreach (var customer in
from c in customers
where !String.IsNullOrEmpty(c.Email)
select c)
{
Console.WriteLine("發送郵件給 {0}", customer.Name);
}
//效果同上
foreach (var customer in customers.Where(c => !String.IsNullOrEmpty(c.Email)))
{
Console.WriteLine("發送郵件給 {0}", customer.Name);
}
}
public static void RemoveFromList()
{
Func<List<string>> makeList = () => Enumerable.Range(1, 10000000).Select(n => ("Item " + n + "")).ToList();
var itemsToRemove = new[] { "Item 0", "Item 1", "Item 50", "Item 1000", "Item 999999", "Item 9999999" };
var stopwatch = new Stopwatch();
var list = makeList();
stopwatch.Start();
foreach (var item in itemsToRemove)
{
list.Remove(item);
}
stopwatch.Stop();
Console.WriteLine(list.Count + "Foreach 花了 {0}ms", stopwatch.ElapsedMilliseconds);
list = makeList();
stopwatch.Restart();
var newList = list.Except(itemsToRemove).ToList(); //效率極低
stopwatch.Stop();
Console.WriteLine(newList.Count + "Except 花了 {0}ms", stopwatch.ElapsedMilliseconds);
}
public static void ExceptionHandling()
{
var numbers = Enumerable.Range(1, 10)
.Select(n => 5 - n)
.Select(n =>
{
try
{
return 10 / n;
}
catch (Exception e)
{
Console.WriteLine("拉姆達錯誤: " + e.Message);
return -1;
}
});
foreach (var n in numbers)
{
Console.WriteLine(n);
}
}
}