C#???????????
???????????? ???????[ 2016/4/18 13:49:22 ] ??????????????????? C#
????1. ????
?????????C#?????????????????????????????????д?????????????????????????????????
????????????????????????????lambda?????????lambda??????????????С?????????????????????Щ???C#?????????????????????????????????MulticastDelegate????????????????????????????棺??“????????”?????“???????”??????Χ?????????棬??????????????????????????????Щ??????????????
????2. ???
????????????javascript???????????????????”???????”??????????漰??”???”???????C++11??lambda??java???????????Smalltalk??Block???
??????C#????б????????????????????????
??????1
????int[] scores = { 100?? 80?? 60?? 40?? 20 };
????var min = 60;
????var passed = scores.Where((int i) => { return i >= min; });
??????????Count???????????????????????????????????????min??????????????????????????????????????”?????????free variable??”??????????”bound variable”????????????????????????????????????????????????δ????????????????????????????????min?????????????????????????????
????2.1 ?????????lexical scoping??
????????????????Щ????
????scope?????????“???”??壬???????????????Χ??
????extent?????lifetime????????????????scope????????extent??
????JavaScript?е????????
????????????????????????????javascript?е?“????????”??
??????2
????text = 'global variable';
????function test(){
????alert(text);
????//?2????????????
????var text = "local variable";
????};
????test();
???????????”undefined”??????????????javascript??????????????javascript?е???????????scope???????????????????б?????????????????????Χ???????Ч??????????alert?text????????????????????????????????δ??е????????????text??extent??δ?????text??????Ч??????????????????????Ч???????????”undefined”??
??????????????
????????????????
????“?????????”??о????????(static scoping)??”???”??????????”???”???????????/?????????????????????????????????????“????????????е?????Χ”??
?????????????????????????????????????????????????????????????????????????????????????????????б???????????C#???????
????????????????“???”????“??????”????????洢???????????????ú?????????????????????????У??????????????????“????????????captured variable??”????????????1?У???3?У?????????????????min??????????min???????????????????????????????????????????????????????????????????????????????????????min????????????????????“???”?????????????γ?“???”???????Count??????????????“???”??
????“???”?????????????????????????????????????????????????2?????C#???????н????
??????3
class Program
{
static void Main()
{
foreach(var i in MakeClosures(3))
{
i();
//?????е???????
}
Console.ReadKey();
}
static IEnumerableMakeClosures(int count)
{
var closures = new List();
for (var i = 0; i Add(() => Console.WriteLine(++i));
return closures;
}
}
???????????????????????C#?е?????“?????ò??”???????
??????????д?????????????????i??????????????i?????????i????????????????Щ???????????i
????i??????в?????£??????????????????i == count
??????????δ???????н????”4??5??6″??
????????????????i????????????????????????????????extent?????????????????????????
????????????????????????ò???????????????????java??????????????????????????????????????????????????????????????java????
????public class Main{
????public static void main(String[] args){
????int i = 1;
????Runnable runnable = new Runnable(){
????@Override
????public void run(){
????i = 2;
????}
????};
????runnable.run();
????}
????}
??????????δ???????local variables referenced from an inner class must be final or effectively final??????”????????????????????????final???λ????????final???????????????б?????????”??
??????????????????????????????δ??????runnable.run()??i?????2????java?????????i???????????????????Java????漲????????????????????????
???????????????????????“??????????ò??”???????????????????????????????????
????C++??lambda???????????????????????Щ?????????????????ò??????????????????????????????????”()”??????????????????????????????????
????C#???????????????????
????CLR?????????в????“????????”??“???”??Щ???????C#??????????????????Щ??????????????????????????????????????????????????????????????????????????????????о???????????????????淶?У???????????????????????????????÷????????????
????3. ????С????????????
??????????????”??????/????????”??GoF??????б??????????????????????“????????????????????n??????????”????????
???????磺mvc??model?????????????????????????????????view????????????????????????????????????????????????????????????????????÷???
?????????????????????????
?????????????????????????????????????????????????????????????
????Downloader?????????????????????????
????ProgressView????????棬??????????????
?????????????????????????????????£???????????????????????????????????????????????????????????????????????????????“????????”????????????IProgressMonitor???????????????????????
????????
IProgressMonitor.cs
//?????????
interface IProgressMonitor
{
void OnProgress(int done?? int total);
}
ProgressVeiw.cs
class ProgressVeiw : IProgressMonitor
{
const int LENGTH = 50;
string _last = String.Empty;
public void OnProgress(int done?? int total)
{
var builder = new StringBuilder();
builder.Append('[');
var filled = (int)(done / (total + 0.0) * LENGTH);
for (var i = 0; i Append(i ' : '_');
}
builder.Append(']');
if (done != total)
builder.AppendFormat(" {0:p0}"?? done / (total + 0.0));
else
builder.Append(" ????");
//??????????????
for (var i = 0; i Length; ++i)
Console.Write('b');
var state = builder.ToString();
_last = state;
Console.Write(state);
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11