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);
}
}
}