var result = users.Where(x => x.Age >18).Select(x => x.Name).Distinct().ToList();
var aldultUser = users.Where(x => x.Age>=18);
var allAldultUserName = aldultUser.Select(x => x.Name);
var result = allAldultUserName.Distinct().ToList();
//var r = GetUser(u);
var userToDelete = GetUser(userId);
5. 判斷條件如果能拆開就拆開,這樣絕對方便幫助 debug ,我常常判斷日期的時候會用錯 and , or
這樣拆開會比較方便自己 debug 當條件複雜的時候
if (!string.IsNullOrEmpty(name) && (role == "Admin" || level > 3) && !isLocked)
{
//..
}
var hasName = !string.IsNullOrEmpty(name);
var isPrivileged = role == "Admin" || level > 3;
if (hasName && isPrivileged && !isLocked)
{
//..
}
6. 一些常數值記得抽成變數,不然程式碼一長一定會忘記這數字,而且之後要改可能要改很多地方
const int PassingThres = 87;
if (score > PassingThres)
{
}
目前就是想到這麼多,畢竟寫程式久了很容易偷懶
--
Before you read this, the article was in a superposition of human and AI. Upon observation, the wave function collapsed into a sleep-deprived developer.