1.[按键源码分享]----QUI列表框显示脚本TracePrint输出信息
2.è°ç»ç®åä»ç»ä¸ä¸C#ï¼
3.高分求教vc:listbox多行显示
4.å
³äºc#ä¸UDPç¼ç¨
[按键源码分享]----QUI列表框显示脚本TracePrint输出信息
展示效果:
当Form1加载完成时
Form1中的委托ListBox1列表为空
初始化日志记录并输出信息
再次初始化日志记录并输出信息
定义函数处理日志记录
读取文件"C:\a.log"内容
解析文件内容,去掉换行符并分割
将解析结果加入ListBox1列表中
函数结束
若需获取源码实例,委托flashfxp怎么上传源码推荐关注按键精灵论坛,委托ssr源码知乎知乎账号,委托eclipse手动添加源码微信公众号“按键精灵”。委托ubuntu获取软件源码
如有疑问,委托企业小程序 源码可留言或私信。委托具体内容可点击查看。委托
è°ç»ç®åä»ç»ä¸ä¸C#ï¼
C#(读å "C sharp"ï¼ä¸æè¯é³âå¤æ®â)æ¯å¾®è½¯å ¬å¸åå¸çä¸ç§é¢å对象çãè¿è¡äº.NET Frameworkä¹ä¸çé«çº§ç¨åºè®¾è®¡è¯è¨,委托并å®äºå¨å¾®è½¯èä¸å¼åè 论å(PDC)ä¸ç»å°äº®ç¸.C#æ¯å¾®è½¯å ¬å¸ç 究åAnders Hejlsbergçææ°ææ.C#çèµ·æ¥ä¸Javaæçæ人çç¸ä¼¼;å®å æ¬äºè¯¸å¦åä¸ç»§æ¿,çé¢,ä¸Javaå ä¹åæ ·çè¯æ³,åç¼è¯æä¸é´ä»£ç åè¿è¡çè¿ç¨.ä½æ¯C#ä¸Javaæçææ¾çä¸å,å®åé´äºDelphiçä¸ä¸ªç¹ç¹,ä¸COM(ç»ä»¶å¯¹è±¡æ¨¡å)æ¯ç´æ¥éæç,èä¸å®æ¯å¾®è½¯å ¬å¸.NET windowsç½ç»æ¡æ¶ç主è§.
å¨æ¬æä¸,æå°èå¯å建ä¸ç§æ°è®¡ç®æºè¯è¨çä¸è¬å¨æº,并å°ç¹å«æææ¯ä»ä¹åå 导è´äºC#çåºç°.ç¶åæå°ä»ç»C#åå®ä¸Java,c,c++çç¸ä¼¼ä¹å¤.å ¶æ¬¡æå°è®¨è®ºä¸äºåå¨äºJavaåC#ä¹é´çé«å±æ¬¡ç,ååºç¡çå·®å«.æå°ä»¥è¡¡éå¨ç¨å¤ç§è¯è¨å¼å大ååºç¨ç¨åºçæ¶åæéçç¥è¯(æè 对è¿ç§ç¥è¯ç缺ä¹ç¨åº¦)æ¥ç»ææ¬æ,èè¿æ£æ¯.NETåC#çä¸ä¸ªä¸»è¦æç¥.ç®å,C#å.NETè¿åªè½ä»¥C#è¯è¨è§å,以åWindows çä¸ä¸ª"dé¢è§çæ¬",è¿æMSDNä¸è¿ éå¢å¤çææ¡£éåçå½¢å¼è·å¾(è¿æ²¡ææç»å®å).
微软c#è¯è¨å®ä¹ä¸»è¦æ¯ä»CåC++继æ¿èæ¥ç,èä¸è¯è¨ä¸ç许å¤å ç´ ä¹åæ äºè¿ä¸ç¹.C#å¨è®¾è®¡è ä»C++继æ¿çå¯éé项æ¹é¢æ¯Javaè¦å¹¿æ³ä¸äº(æ¯å¦è¯´structs),å®è¿å¢å äºèªå·±æ°çç¹ç¹(æ¯æ¹è¯´æºä»£ç çæ¬å®ä¹).ä½å®è¿å¤ªä¸æç,ä¸å¯è½æ¤å®Java.C#è¿éè¦è¿åæä¸ç§å¼åè è½å¤æ¥ååéç¨çè¯è¨.è微软å½å为å®çè¿ç§æ°è¯è¨å¤§é 声å¿ä¹æ¯å¼å¾æ³¨æç.ç®å大家çååºæ¯:"è¿æ¯å¯¹Javaçåå»."
C#æ´è±¡Javaä¸äº,è½ç¶å¾®è½¯å¨è¿ä¸ªé®é¢ä¸ä¿ææ²é».è¿ä¹æ¯ææä¸çäºæ ,æè§å¾,å 为Javaè¿æ¥å¾æåè使ç¨Javaçå ¬å¸é½æ¥å说å®ä»¬å¨ç产æçä¸æ¯C++è·å¾äºæé«.
Javaæ带æ¥ç巨大影åå大家对å®ç广æ³æ¥åå·²ç»ç±å·¥ä½äºè¿ç§è¯è¨åå¹³å°ä¹ä¸çç¨åºåæ°éææ¾ç说æäº(估计ä¸çèå´å å ±æ两ç¾äºåä¸ç¨åºå使ç¨Java).ç±è¿ç§è¯è¨åæçåºç¨ç¨åºçæ°éæ¯ä»¤äººæ讶ç并已ç»æ¸éäºæ¯ä¸ä¸ªçº§å«ç计ç®,å æ¬æ 线计ç®å移å¨çµè¯(æ¯å¦æ¥æ¬åæçJavaçµè¯).C#è½å¤å¨ç¨æ·é¢åè·å¾è¿æ ·ç礼éå?æä»¬å¿ é¡»çå¾ å¹¶è§æ,就象已ç»ç±SSIå ¬å¸çCEOå主å¸Kalpathi S. Sureshæåºæ¥çé£æ ·,"æåç°ææè¿äºé½æ¯æ¸è¿ç.å¦æC#ä¸åå¨,æ们æ»è½åå°JavaæCåC++.è¿äºé½ä¸å®å ¨æ¯æ°ææ¯;å®ä»¬å¨æ´å¤§çæä¹ä¸æ¥è¯´åªæ¯å¤§å ¬å¸å¶é çå¸åºå±å¤´.æä»¬å¿ é¡»ç»ä»ä»¬æ¶é´å®é¡¿ä¸æ¥ççè¿äºæ¯ä¸æ¯çç对ITå·¥ä¸æä»ä¹å½±å."
C#ä»Java继æ¿èæ¥çç¹ç¹
ç±»:å¨C#ä¸ç±»çç³æä¸Javaå¾ç¸ä¼¼.è¿æ¯åççå 为ç»éªåè¯æ们Java模åå·¥ä½å¾å¾å¥½.Javaçå ³é®åimportå·²ç»è¢«æ¿æ¢æusing,å®èµ·å°äºåæ ·çä½ç¨.ä¸ä¸ªç±»å¼å§æ§è¡çèµ·ç¹æ¯éææ¹æ³Main().ä¸é¢çHello Worldç¨åºå±ç¤ºäºåºæ¬çå½¢å¼:
using System;
class Hello
{
static void Main()
{
Console.WriteLine("Hello, world");
}
}
å¨è¿ä¸ªä¾åä¸,Systemè¿ä¸ªååæåä¸ä¸ªå æ¬äºåºæ¬C#å®ç¨ç±»éåçå½å空é´(namespace).è¿ä¸ªå½å空é´å æ¬äºConsoleç±»,å®å¨è¿ä¸ªä¾åä¸è¢«ç¨æ¥è¾åºä¸ä¸ªå符串.ç±»å¯ä»¥æ¯æ½è±¡çåä¸å¯ç»§æ¿ç:ä¸ä¸ªè¢«ç³ææabstractçç±»ä¸è½è¢«å®ä¾å;å®åªè½è¢«ç¨åä¸ä¸ªåºç±».C#å ³é®åsealed就象Javaå ³é®åfinal,å®ç³æä¸ä¸ªç±»ä¸æ¯æ½è±¡ç,ä½æ¯å®ä¹ä¸è½è¢«ç¨åå¦ä¸ä¸ªç±»çåºç±».çé¢:就象å¨Javaä¸ä¸æ ·,ä¸ä¸ªçé¢æ¯ä¸ç»æ¹æ³éåçæ½è±¡å®ä¹.å½ä¸ä¸ªç±»æç»æä½å®ç°ä¸ä¸ªçé¢çæ¶å,å®å¿ é¡»å®ç°è¿ä¸ªçé¢ä¸å®ä¹çæææ¹æ³.ä¸ä¸ªåä¸çç±»å¯ä»¥å®ç°å 个çé¢.ä¹è®¸ä»¥åä¼åºç°ä¸äºå¾®å¦çå·®å«,ä½æ¯è¿ä¸ªç¹ç¹çèµ·æ¥ä¸Javaç¸æ¯æ²¡æåå.å¸å°è¿ç®:æ¡ä»¶è¡¨è¾¾å¼çç»ææ¯å¸å°æ°æ®ç±»å,å¸å°æ°æ®ç±»åæ¯è¿ç§è¯è¨ä¸ç¬ç«çä¸ç§æ°æ®ç±»å.ä»å¸å°ç±»åå°å ¶ä»ç±»å没æç´æ¥ç转æ¢è¿ç¨.å¸å°å¸¸étrueåfalseæ¯C#ä¸çå ³é®å.é误å¤ç:å¦Javaä¸é£æ ·,éè¿æåºåææå¼å¸¸å¯¹è±¡æ¥ç®¡çé误å¤çè¿ç¨.å å管ç:ç±åºå±.NETæ¡æ¶è¿è¡èªå¨å ååå¾åæ¶.
C#ä»CåC++继æ¿çç¹ç¹
ç¼è¯:ç¨åºç´æ¥ç¼è¯ææ åçäºè¿å¶å¯æ§è¡å½¢å¼.ä½C#çæºç¨åºå¹¶ä¸æ¯è¢«ç¼è¯æäºè¿å¶å¯æ§è¡å½¢å¼ï¼èæ¯ä¸ä¸ä¸é´è¯è¨ï¼ç±»ä¼¼äºJAVAåèç ãå¦æåé¢çHello Worldç¨åºè¢«ä¿åæä¸ä¸ªææ¬æ件并被å½å为Hello.cs,å®å°è¢«ç¼è¯æå½åHello.exeçå¯æ§è¡ç¨åº.
ç»æä½:ä¸ä¸ªC#çç»æä½ä¸C++çç»æä½æ¯ç¸ä¼¼ç,å 为å®è½å¤å å«æ°æ®å£°æåæ¹æ³.ä½æ¯,ä¸è±¡C++,C#ç»æä½ä¸ç±»æ¯ä¸åçèä¸ä¸æ¯æ继æ¿.ä½æ¯,ä¸Javaç¸åçæ¯,ä¸ä¸ªç»æä½å¯ä»¥å®ç°çé¢.
é¢ç¼è¯:C#ä¸åå¨é¢ç¼è¯æ令æ¯ææ¡ä»¶ç¼è¯,è¦å,é误æ¥ååç¼è¯è¡æ§å¶.å¯ç¨çé¢ç¼è¯æ令æ:
#define
#undef
#if
#elif
#else
#endif
#warning
#error
#line []
没æäº#include 伪æ令.ä½ æ æ³åç¨#define è¯å¥å¯¹ç¬¦å·èµå¼,æ以就ä¸åå¨æºä»£ç æ¿æ¢çæ¦å¿µ--è¿äºç¬¦å·åªè½ç¨å¨#ifå#elif伪æ令é.å¨#line伪æ令éçæ°å(åå¯éçåå)è½å¤ä¿®æ¹è¡å·è¿æ#warningå#errorè¾åºç»æçæ件å.
æä½ç¬¦éè½½:ä¸äºæä½ç¬¦è½å¤è¢«éè½½,èå¦ä¸äºåä¸è½.ç¹å«çæ¯,没æä¸ä¸ªèµå¼è¿ç®ç¬¦è½å¤è¢«éè½½.è½å¤è¢«è¢«éè½½çåç®æä½ç¬¦æ¯:
+ - ! ~ ++ -- true false
è½å¤è¢«éè½½çäºå è¿ç®ç¬¦æ¯:
+ - * / % & | ^ << >> == != > < >= <=
C#ç¬æçç¹ç¹
C#æå¼äººå ¥èçå°æ¹æ¯å®åJavaçä¸å,èä¸æ¯ç¸ä¼¼çå°æ¹.è¿ä¸è(åè¿ä¸ªç³»å第äºé¨åç大é¨åå°æ¹)讲述äºC#å®ç°çåJavaä¸åçå°æ¹æè Javaæ ¹æ¬æ²¡æçç¹ç¹.
ä¸é´ä»£ç :微软å¨ç¨æ·éæ©ä½æ¶MSILåºè¯¥ç¼è¯ææºå¨ç çæ¶åæ¯çäºå¾å¤§çä½å°.å¾®è½¯å ¬å¸å¾å°å¿ç声称MSILä¸æ¯è§£éæ§ç,èæ¯è¢«ç¼è¯æäºæºå¨ç .å®ä¹æç½è®¸å¤--å¦æä¸æ¯å¤§å¤æ°çè¯--ç¨åºå认为Javaç¨åºè¦ä¸å¯é¿å çæ¯Cç¼åçä»»ä½ä¸è¥¿é½è¦æ ¢.èè¿ç§å®ç°æ¹å¼å³å®äºåºäºMSILçç¨åº(æçæ¯ç¨C#,Visual Basic,"Managed C++"--C++çä¸ä¸ªç¬¦åCLSççæ¬--çè¯è¨ç¼åçç¨åº)å°å¨æ§è½ä¸è¶ è¿"解éæ§ç"Java代ç .å½ç¶,è¿ä¸ç¹è¿éè¦å¾å°äºå®è¯æ,å 为C#åå ¶ä»çæMSILçç¼è¯å¨è¿æ²¡æåå¸.ä½æ¯Java JITç¼è¯å¨çæ®éåå¨ä½¿å¾JavaåC#å¨æ§è½ä¸ç¸å¯¹ç¸å.象"C#æ¯ç¼è¯è¯è¨èJavaæ¯è§£éæ§ç,"ä¹ç±»ç声æåªæ¯åä¸æå·§.Javaçä¸é´ä»£ç åMSILé½æ¯ä¸é´çæ±ç¼å½¢å¼çè¯è¨,å®ä»¬å¨è¿è¡æ¶æå ¶å®çæ¶å被ç¼è¯ææºå¨ä»£ç .
å½å空é´ä¸çç³æ:å½ä½ å建ä¸ä¸ªç¨åºçæ¶å,ä½ å¨ä¸ä¸ªå½å空é´éå建äºä¸ä¸ªæå¤ä¸ªç±».åå¨è¿ä¸ªå½å空é´é(å¨ç±»çå¤é¢)ä½ è¿æå¯è½å£°æçé¢,æ举类ååç»æä½.å¿ é¡»ä½¿ç¨usingå ³é®åæ¥å¼ç¨å ¶ä»å½å空é´çå 容.
åºæ¬çæ°æ®ç±»å:C#æ¥ææ¯C,C++æè Javaæ´å¹¿æ³çæ°æ®ç±»å.è¿äºç±»åæ¯bool, byte, ubyte, short, ushort, int, uint, long, ulong, float, double,ådecimal.象Javaä¸æ ·,ææè¿äºç±»åé½æä¸ä¸ªåºå®ç大å°.å象CåC++ä¸æ ·,æ¯ä¸ªæ°æ®ç±»åé½ææ符å·åæ 符å·ä¸¤ç§ç±»å.ä¸Javaç¸åçæ¯,ä¸ä¸ªå符åéå å«çæ¯ä¸ä¸ªä½çUnicodeå符.C#æ°çæ°æ®ç±»åæ¯decimalæ°æ®ç±»å,对äºè´§å¸æ°æ®,å®è½åæ¾ä½è¿å¶æ°å.
两个åºæ¬ç±»:ä¸ä¸ªåå«objectçç±»æ¯ææå ¶ä»ç±»çåºç±».èä¸ä¸ªåå«stringçç±»ä¹è±¡objectä¸æ ·æ¯è¿ä¸ªè¯è¨çä¸é¨å.ä½ä¸ºè¯è¨çä¸é¨ååå¨æå³çç¼è¯å¨æå¯è½ä½¿ç¨å®--æ 论ä½æ¶ä½ å¨ç¨åºä¸åå ¥ä¸å¥å¸¦å¼å·çå符串,ç¼è¯å¨ä¼å建ä¸ä¸ªstring对象æ¥ä¿åå®.
åæ°ä¼ é:æ¹æ³å¯ä»¥è¢«å£°ææ¥åå¯åæ°ç®çåæ°.缺ççåæ°ä¼ éæ¹æ³æ¯å¯¹åºæ¬æ°æ®ç±»åè¿è¡å¼ä¼ é.refå ³é®åå¯ä»¥ç¨æ¥å¼ºè¿«ä¸ä¸ªåééè¿å¼ç¨ä¼ é,è¿ä½¿å¾ä¸ä¸ªåéå¯ä»¥æ¥åä¸ä¸ªè¿åå¼.outå ³é®åä¹è½å£°æå¼ç¨ä¼ éè¿ç¨,ä¸refä¸åçå°æ¹æ¯,å®ææè¿ä¸ªåæ°å¹¶ä¸éè¦åå§å¼.
ä¸COMçéæ:C#对Windowsç¨åºæ大çåç¹å¯è½å°±æ¯å®ä¸COMçæ ç¼éæäº,COMå°±æ¯å¾®è½¯çWinç»ä»¶ææ¯.å®é ä¸,æç»æå¯è½å¨ä»»ä½.NETè¯è¨éç¼åCOM客æ·åæå¡å¨ç«¯.C#ç¼åçç±»å¯ä»¥åç±»åä¸ä¸ªä»¥åå¨çCOMç»ä»¶;çæçç±»ä¹è½è¢«ä½ä¸ºä¸ä¸ªCOMç»ä»¶ä½¿ç¨,ç¶ååè½ä½¿ç¨,æ¯æ¹è¯´,JScriptè¯è¨åç±»åå®ä»èå¾å°ç¬¬ä¸ä¸ªCOMç»ä»¶.è¿ç§ç°è±¡çç»ææ¯å¯¼è´äºä¸ä¸ªè¿è¡ç¯å¢ç产ç,å¨è¿ä¸ªç¯å¢éçç»ä»¶æ¯ç½ç»æå¡,å¯ç¨ç¨ä»»ä½.NETè¯è¨åç±»å.
ç´¢å¼ä¸æ :ä¸ä¸ªç´¢å¼ä¸å±æ§é¤äºä¸ä½¿ç¨å±æ§åæ¥å¼ç¨ç±»æåèæ¯ç¨ä¸ä¸ªæ¹æ¬å·ä¸çæ°åæ¥å¿åå¼ç¨(就象ç¨æ°ç»ä¸æ ä¸æ ·)以å¤æ¯ç¸ä¼¼ç.
public class ListBox: Control
{
private string[] items;
public string this[int index]
{
get
{
return items[index];
}
set
{
items[index] = value;
Repaint();
}
}
}
å¯ä»¥ç¨ä¸ä¸ªå¾ªç¯å¨æ¥å¿åå¼ç¨å符串å é¨æ°ç»æå,就象ä¸é¢è¿æ ·:
ListBox listBox = ...;
listBox[0] = "hello";
Console.WriteLine(listBox[0]);
代çååé¦:ä¸ä¸ªä»£ç对象å æ¬äºè®¿é®ä¸ä¸ªç¹å®å¯¹è±¡çç¹å®æ¹æ³æéçä¿¡æ¯.åªè¦æå®å½æä¸ä¸ªèªæçæ¹æ³æéå°±è¡äº.代ç对象å¯ä»¥è¢«ç§»å¨å°å¦ä¸ä¸ªå°æ¹,ç¶åå¯ä»¥éè¿è®¿é®å®æ¥å¯¹å·²åå¨çæ¹æ³è¿è¡ç±»åå®å ¨çè°ç¨.ä¸ä¸ªåé¦æ¹æ³æ¯ä»£ççç¹ä¾.eventå ³é®åç¨å¨å°å¨äºä»¶åççæ¶å被å½æ代çè°ç¨çæ¹æ³å£°æ.
è¡¥å ï¼
C#ç®å²ââæèªãç¨åºåãæå¿-æå
C# ç®å²
ç¼è æï¼æ¶é´è¿å¾çå¿«ï¼å± ç¶ç°å¨å°±å¯ä»¥åC#çç®å²äºãä½æ¯æ³æ³ä¹ä¸å¥æªï¼C#å¯è°
èµ·ç¹é«ãåå±å¿«çæ°ä¸ä»£è¯è¨ï¼å®çè¿äºå¹´èµ°è¿äºå¾å¤åè¾åå å¹´çè·¯ãå ¬å å°è¯´ï¼C#æ¯ç®
åå ¼é¡¾ç³»ç»å¼åååºç¨å¼åçæä½³å®ç¨è¯è¨ï¼å¹¶ä¸å¾æå¯è½æ为ç¼ç¨è¯è¨åå²ä¸ç第ä¸ä¸ªâå ¨
è½âåè¯è¨ãçè¿è¿ç¯ç®å²ï¼æ们é½åºè¯¥æç½ï¼ä¸è¦åæC#çæå¹´è½»åçäºââåªè¦æ¯â马
æå¤çº³âï¼å°±æ©æå½âççâã
C# 1.0ï¼çº¯ç²¹çé¢å对象
å½æ¶é´å溯å°å¹´åºï¼å¾®è½¯æ£å¨å¿äºæ°ä¸ä»£COMç设计工ä½ãæ¤åï¼COMä¸ç´æ¯ç»ä»¶åå¼åä¸é常æåçä¸ç§ææ¯ï¼ä½ç±äºå®ä» æä¾äºäºè¿å¶
å±é¢ä¸çç»ä¸ï¼å æ¤æ æ³å°ç±»åä¿¡æ¯åç¨äºæ¯æåºç¡å¹³å°åå¼åå·¥å ·çä¿¡æ¯æ¾å°ç»ä»¶ä¸ãè¿æ¶ï¼Javaæ£å¨éæ¥èµ°åæçãäºæ¯ï¼å¾®è½¯å¦ä¹ Java
çåæ³ï¼å°èææºçæ¦å¿µå¼å ¥å°äºCOMé¢åï¼åæ¶ï¼å¾®è½¯æåºäºâå æ°æ®âçæ¦å¿µï¼ç¨äºæè¿°ç»ä»¶çç±»åä¿¡æ¯åå·¥å ·æ¯æä¿¡æ¯ï¼å¹¶å³å®å°å ¶æ¾å ¥
å°ç»ä»¶å½ä¸ãè¿ç§âCOMèææºâçååå¨ç»åäºè¥å¹²äºè®ºåï¼æç»è¢«å®ä¸ºCLRï¼Common Language Runtimeï¼å ¬å ±è¯è¨è¿è¡æ¶ï¼ãä¸æ¤åæ¶ï¼å¾®
软æåºäºå¨è¯¥è¿è¡æ¶ä¸è¿ä½çè¯è¨åºè¯¥éµå¾ªçä¸äºè§åï¼ä»¥å该èææºçç±»åç³»ç»åæ令éââææè¿äºè§èå½¢æäºæç»çC L Iï¼Common
Language Infrastructureï¼å ¬å ±è¯è¨åºç¡è®¾æ½ï¼ï¼å¹¶æ交ç»äºECMAå§åä¼ãåæ¶ï¼å¾®è½¯å¼åäºCLIçä¸ä¸ªå®ç°ï¼è¿å°±æ¯å¤§åé¼é¼ç.NETäºã
å¹´æï¼å¾®è½¯å¯å¨äºä¸ä¸ªå ¨æ°çè¯è¨é¡¹ç®ââCOOLï¼è¿æ¯ä¸æ¬¾ä¸é¨ä¸ºCLR设计ç纯é¢å对象çè¯è¨ï¼ä¹æ£æ¯æ¬æç主è§ââC#çå身ãåæ¶
åå¹´æä½ï¼å¹´7æ份ï¼å¾®è½¯å®æäºCOOLè¯è¨çä¸ä¸ªå é¨çæ¬ãç´å°å¹´2æ份ï¼å¾®è½¯ææ£å¼å°COOLè¯è¨æ´å为C#ãæ®è¯´èµ·è¿ä¸ªååæ¯å 为
C#å¼åå°ç»ç人å¾è®¨åæç´¢å¼æï¼å æ¤æ大é¨åæç´¢å¼ææ æ³è¯å«çâ#â å符ä½ä¸ºè¯¥è¯è¨ååçä¸é¨åï¼è¿æä¸ç§è¯´æ³æ¯å¨é³ä¹å½ä¸â#âæ¯
åè°è®°å·ï¼è¡¨è¾¾äºå¾®è½¯å¸æå®å¨Cçåºç¡ä¸æ´ä¸ä¸å±æ¥¼çç¾å¥½æ¿æââå½ç¶è¿äºé½åªæ¯ä¼ 说ï¼æ ä»èè¯ãåæ¯åç»äºä¸ç³»åçä¿®æ¹ï¼å¾®è½¯ç»äºå¨
å¹´7æåå¸äºC#è¯è¨ç第ä¸ä¸ªé¢è§çãå æ¤äººä»¬ä¸è¬è®¤ä¸ºC#æ¯å¹´åå¸çï¼å¹¶ä»¥æ¤æ¥è®¡ç®å®çâå¹´é¾âãå¨æ¤åçä¸å¹´å¤æ¶é´éï¼å¾®è½¯ä¸
ç´å¨ä¿®è¡¥å个æµè¯çæ¬ä¸çBUGãç´å°å¹´2æï¼å¾®è½¯ç»äºæ¨åºäºè¿è¿æªä¸å¸çVisual Studio 7.0ï¼å¹¶å°å ¶å®å为âVisualStudio .NET
âãéçè¿å¥å¼åç¯å¢çåºçï¼å¼åè 们ç»äºçå°äºC#è¯è¨ç第ä¸ä¸ªæ£å¼çæ¬ââC# 1.0ãæ¤åï¼å¾®è½¯é©¬ä¸åè¹ï¼Visual Studioä¹æ¢å¤äºå¾æ¥
çå¼åè¿åº¦ãå¨å¹´5æï¼å¾®è½¯å¦ææ¨åºäºVisual Studio .NET ï¼åæ¶ä¹åå¸äºC#çæ¹è¿çæ¬ââC# 1.1ãè¿ä¸æ¶æçC#ï¼ä»¥ä¸ç§°ä¸ºC#
1.xï¼æåºäºçº¯ç²¹çé¢å对象æ¦å¿µï¼å¹¶å¨è¯è¨ç¹æ§ä¸å±ç°å¾æ·æ¼å°½è´ãC++并é纯é¢å对象çï¼ä¸ºäºåCå ¼å®¹ä»¥åæä¾æ´é«çæ§è¡æçï¼å®ä¿çäº
å¾å¤æ¨¡ååçä¸è¥¿ãJava尽管å·ç§°æ¯é¢å对象çï¼ä½å®é ä¸ï¼å¯¹äºå¯¹è±¡æåºè¯¥å ·å¤çä¸ç§ææç»æââå±æ§ãæ¹æ³åäºä»¶ï¼Javaä» æä¾äºæ¹æ³
ï¼å ¶å®ä¸¤ç§ç»æé½è¦éè¿æ¹æ³æ¥æ¨¡æãå¨C# 1.xä¸ï¼ææé¢å对象çæ¦å¿µé½å¨è¯è¨ä¸å¾å°äºé常好çä½ç°ãåæ¶ï¼C#è¿éè¿ç±»ç±»åãå¼ç±»åå
æ¥å£ç±»åçæ¦å¿µå½¢æäºç»ä¸çç±»åç³»ç»ãC#使ç¨äºå¤§å®¶æçç¥çè¯æ³å®ç°äºæ¹æ³ï¼ä»¥è³äºå¾å¤äººè®¤ä¸ºC#åJavaãC++çé¢å对象è¯è¨âé常ç¸å
âï¼è¿ä½¿å¾ä»ä½¿ç¨å ¶ä»é¢å对象è¯è¨è½¬å°ä½¿ç¨C#çè¿ç¨é常ç®åãæ¤å¤ï¼C#è¿éè¿æ åæ°å表çæ¹æ³å£°åè¯æ³ï¼ç»åget/set访é®å¨å®ç°äºä¼é
çå±æ§è¯æ³ãå ¶ä¸çget访é®å¨ç¸å½äºè·åå±æ§å¼çæ¹æ³ï¼å¯ä»¥éè¿ä¸äºè¿ç®è¿åæç»çç»æï¼èä¸æ¯ç®åå°è¿åä¸ä¸ªåéçå¼ï¼èset访é®å¨
ç¸å½äºè®¾ç½®å±æ§å¼çæ¹æ³ï¼å¨å ¶ä¸å¯ä»¥è¿è¡ä¸ç³»åæ£æµï¼æåå°å±æ§å¼èµç»ç¸åºçåéãåæ¶ï¼éè¿åæ¶æä¾getåset访é®å¨ãåªæä¾get访é®
å¨ååªæä¾set访é®å¨ï¼è¿å¯ä»¥å¾æ¹ä¾¿å°å®ç°å¯è¯»åãåªè¯»ååªåçå±æ§ãC#çè¿ç§å±æ§è¯æ³ï¼ä½¿å¾ä¸ä¸ªå±æ§å¨æä¾è¯¥å±æ§çç±»çå é¨çæ¥ï¼é
常åä¸ç»æ¹æ³ï¼è对äºå¤é¨è°ç¨ç±»çæ¥ï¼è®¿é®ä¸ä¸ªå¯¹è±¡çå±æ§å访é®å®çå ¬å ±å没æä»»ä½åºå«ã
éè¿å§æï¼ç¨åä»ç»ï¼ï¼ç»åå ³é®åeventï¼C#æä¾äºä¼é çäºä»¶æ¦å¿µã使ç¨+=è¿ç®ç¬¦ï¼å¼åè å¯ä»¥é常æ¹ä¾¿å°å°ä¸ä¸ªäºä»¶å¤çå¨å ³èå°ä¸ä¸ªäº
件ä¸ï¼è¿ä¸ªè¿ç¨ç§°ä¹ä¸ºâ订é âä¸ä¸ªäºä»¶ãç±äºå§æå é¨å°è£ äºä¸ä¸ªè°ç¨é¾è¡¨ï¼å æ¤å¯ä»¥æ¹ä¾¿å°ä¸ºä¸ä¸ªäºä»¶æ·»å å¤ä¸ªäºä»¶å¤çå¨ï¼è¿äºå¤çå¨
ä¼èªå¨å°ä¾æ¬¡è°ç¨ãå¤å¹´çå¼åè¯è¨è¿åè¯æï¼å½æ°æéæ¯é常éè¦ä¹æ¯é常å±é©çè¯è¨ç¹å¾ä¹ä¸ãåæ¶ï¼åºäºå½æ°æéçåè°æºå¶ä¹Windows
æ ¸å¿æ¦å¿µä¹ä¸ãç¶èï¼ç±äºå½æ°æéå¾é¾éªè¯åæ°çç±»ååç¡®æ§ï¼å æ¤C#ï¼ç¡®åå°è¯´æ¯CLIï¼æåºäºâå§æâçæ¦å¿µï¼è¿æ¯ä¸ç§ç±»åå®å ¨çå½æ°
æéé¾è¡¨ãè¿æå³çï¼C#ä¸ä» å¯ä»¥æä¾åè°æºå¶ï¼åæ¶è°ç¨åè°çä¸æ¹è¿æ éå¨å ¶å é¨ç»´æ¤å½æ°æéå表ï¼æè¦åçä» ä» æ¯å£°åä¸ä¸ªå ·ææ°å½
å§æç±»åçå ¬å ±æåå³å¯ï¼èæä¾åè°çä¸æ¹ä¹åªééè¿æé ä¸ä¸ªå¸¦ææå®æ¹æ³çç¸åºå§æå®ä¾ï¼å¹¶éè¿â+=âè¿ç®ç¬¦æ·»å å°åè°å表å³å¯ã
尽管C# 1.xæä¾äºå¦æ¤å¤çæ°é²æ¦å¿µï¼ä½å®é ä¸ï¼è¿äºæ¦å¿µé½æ¯ç±CLIæåºçãå æ¤å½å°ä¸ä¸ªC#æºç¨åºç¼è¯ä¸ºå¯æ§è¡æ件æ¶ï¼ç¼è¯å¨åçå·¥ä½ç¸
对èè¨å¹¶ä¸å¤ãéè¦ç¼è¯å¨ä»£å³çæ¯è¦å°ä¸ä¸ªç®åçå§æå®ä¹è¯å¥ç¿»è¯ä¸ºä¸ä¸ªç»§æ¿èªSystem.MulticastDelegateç±»åå®ä¹ã
C# 2.0ï¼æ³åç¼ç¨æ°æ¦å¿µ
微软æ¬æç®ç»§ç»ä¿è¯å¼åè¿åº¦ï¼å¹¶å¨å¹´æ¨åºVisual Studio .NET ï¼ä½ç±äºå ¶é´è½¯ä»¶å·¥ç¨å¦å°¤å ¶æ¯è½¯ä»¶ç®¡çå¦ç大è§æ¨¡è¿æ¥ï¼å¾®è½¯ææ
ä¾çè¿ç§ä» å ·å¤å¼ååè°è¯åè½çIDEå·²ç»æ æ³æ»¡è¶³å¢éå¼åçéæ±ãå æ¤å¾®è½¯å³å®å¨é¡¹ç®è®¾è®¡å管çå·¥å ·æ¹é¢è¿è¡äºè¿ä¸æ¥ç åï¼å¹¶å°å ¶éæ
å°Visual Studioä¸ï¼ä»¥èµ¢ååæçå¸åºãå æ¤ï¼å¾®è½¯å°Visual Studio.NET âæ¹åâ为Visual Studio ï¼å¹¶å³å®æ¨è¿ä¸å¹´åå¸ãä¸è¿
ï¼å¾®è½¯è¿æ¯åæå¨å¹´ç6æ份åå¸äºVisual Studioç第ä¸ä¸ªBeta çï¼åæ¶åå¼åè å±ç¤ºäºC#è¯è¨ç2.0çæ¬ãå¹´4æï¼å¾®è½¯åå¸äº
Visual Studio Beta2ï¼è¿å·²ç»æ¯å ·å¤äºå ä¹å ¨é¨åè½çVisualStudioï¼å æ¬ç产åæSQL ServerãTeam Foundation Serverå
TeamSuiteãè¿æ¶çC#ç¼è¯å¨å·²ç»è½å¤å¤çC# 2.0ä¸ææçæ°ç¹æ§ãC# 2.0为å¼åè 带æ¥çæ主è¦çç¹æ§å°±æ¯æ³åç¼ç¨è½åãåé¢å对象ææ³ä¸
æ ·ï¼æ³åææ³ä¹æ¯ä¸ç§å·²ç»æççç¼ç¨ææ³ï¼ä½ä¾ç¶æ¯æ²¡æåªä¸ç§ä¸»æµå¼åè¯è¨è½å¤æ¯æå®å¤çæ³åæ¦å¿µãè¿ä¸»è¦æ¯å 为æ³åçæ¦å¿µå¨ä¸å®ç¨
度ä¸å¯¹é¢å对象æ¦å¿µè¿è¡å²å»ï¼åæ¶ï¼ç±äºå¨ç¼è¯æé´å¯¹ç±»ååæ°çå®å ¨æ£æµå¾é¾åå°ï¼å¾å¤é®é¢ä¼è¢«éçå°è¿è¡æ¶ãC# 2.0å«åºå¿è£ï¼å¯¹æ³
åç±»ååæ°æåºäºâ约æâçæ°æ¦å¿µï¼å¹¶ä»¥ä¼é çè¯æ³ä½ç°å¨è¯è¨ä¹ä¸ãæäºçº¦æï¼ç»åç¼è¯å¨å¼ºå¤§çç±»åæ¨æè½åï¼å¯ä»¥å¨ç¼è¯æ¶
åç°å ä¹ææâå±é©âçæ³ååºç¨ãC# 2.0çå¦ä¸ä¸ªçªåºçç¹æ§å°±æ¯å¿åæ¹æ³ï¼ç¨æ¥å代ä¸äºçå°ç并ä¸ä» åºç°ä¸æ¬¡çå§æï¼ä½¿å¾è¯è¨ç»ææ´
å ç´§åãå¿åæ¹æ³é¤äºå¯ä»¥ä½¿å¾äºä»¶å¤çå¨çç¼åæ´å ç²¾ç®ä»¥å¤ï¼è¿å°å¼åè å¸¦å ¥äºç¨åºè®¾è®¡çä¸ä¸ªæ°çé¢åââå½æ°å¼ç¼ç¨ï¼æ¾ç»æé«äººå°±
ç¨å¿åæ¹æ³ç»åæ³åç¼ç¨å®ç°äºå½æ°å¼ç¼ç¨ä¸çéè¦ç»æââ Lambda 表达å¼ã尽管è¿ç§å®ç°æ¾å¾å¾ç¹çèä¸ä¸æç解ï¼ä½æ¯ç«æ¯å®ç°äºãæ
ç»ï¼å½æ°å¼ç¼ç¨è¿æ¯è¢«å¼å ¥å°äºC#è¯è¨ä¸ï¼è¿å°å¨ä¸ä¸èä¸ä¸ºå¤§å®¶è®²è¿°ã
æ¤å¤ï¼C# 2.0è¿è¿ä¸æ¥å¢å¼ºäºè¯è¨ç表达è½åãå¨C# 2.0ä¸ï¼å±æ§è¯æ³ä¸çgetåset访é®å¨å¯ä»¥æ¥æä¸åçæéï¼è¿å°±ä½¿å¾å®ä¹ä¸ä¸ªå¨åºçå
é¨å¯è¯»åï¼èå¨åºçå¤é¨åªè¯»çå±æ§æ为å¯è½ãåæ¶ï¼C# 2.0è¿æä¾äºè¿ä»£å¨çæ¦å¿µï¼è¿ä½¿å¾ä¸ä¸ªç±»æ éå®ç°IEnumerator åIEnumerableæ¥å£
å³å¯å®ç°ä¸ä¸ªå¯ä»¥è¿è¡éåçç±»åï¼å¹¶ä¸æ éå¨ç±»åä¸ç»´æ¤è¿ä»£ç¶æãæ¤æ¶ç.NETå·²ç»å¾å°äºå¾å¹¿æ³ç认å¯ï¼å¹¶ä¸å 为å æ°æ®ä¸ºç»ä»¶å¸¦æ¥äºå¼º
大çèªææè¿°è½åï¼è®¸å¤ç¨åºåºåå被å¸å¼å°.NETå¹³å°ä¸æ¥ãéç.NETç¨åºåºæ°éçå¢é¿ï¼éæ¸æ´é²äºå½åçé®é¢ãå¨é¢å对象ææ¯å¹¿æ³åå±
åï¼äººä»¬å°±æè¯å°ååç管çé®é¢ï¼å æ¤å ä¹ææçé¢å对象è¯è¨é½æåºäºâå½å空é´âçæ¦å¿µï¼
èå¨C# 1.xæ¶ä»£ï¼è¿ä¸ªé®é¢åä¸æ¬¡åºç°ãå¦æä¸ä¸ªåºååXX å¸æ以XX.Systemæ¥å½åä»ä»¬èªå·±çç³»ç»åºç¡åºï¼é£ä¹å½å¼åè 使ç¨using System
è¯å¥æ¶å°±ä¼äº§çæ§ä¹ã为æ¤ãC# 2.0ä¸æä¾äºglobalå ³é®åï¼è¿ä¸º.NETåºä¸ææçå½å空é´æä¾äºä¸ä¸ªâæ ¹â,éè¿æå®global::Systemå
global::XX.Systemå°±å¯ä»¥åºå«ä¸¤ä¸ªåºäºãè¿ä¸æ¶æçC#ç¼è¯å¨åå¾é常å¤æï¼æ³åçå¼å ¥ä½¿å¾ç¼è¯å¨ä¸å¾ä¸å ·å¤è¶ 强çç±»åæ¨æè½åãåæ¶ï¼
è¿ä»£å¨çææ³å¹¶éæ¯å¨CLIå±é¢ä¸å®ç°çï¼èæ¯ç±ç¼è¯å¨èªå¨çæäºå®ç°I E n u m e r a t o r åIEnumerableæ¥å£ç±»åã
C# 3.0ï¼é鬼
å¨ç»åäºä¸ç³»åçæ¹è¿åå®ååï¼å¾®è½¯å³å®äºå¹´æåå¸Visual Studioï¼è¯¥å¼åç¯å¢å°æ£å¼æ¯æC#2.0ãç±äºæ¤åæ¨åºäºæ°ä¸ªé¢è§ç
åæµè¯çï¼å¤§å®¶çæå¾ ä¹æ ä¼¼ä¹å·²ç»ä¸æ¯é£ä¹å¼ºçäºãèå¹´9 æ份çPDC大ä¼å为å¼åè 们带æ¥äºå¦å¤çæåââC#3.0ï¼ç å代å·âOrcas
âââé鬼ï¼çææ¯é¢è§çã
说å°C# 3.0ï¼å°±ä¸å¾ä¸æä¸ä¸å¾®è½¯çLINQ 项ç®ï¼LINQï¼è¯è¨éææ¥è¯¢ï¼Language Integrated Queryï¼æåºäºä¸ç§éè¿é¢å对象è¯æ³æ¥å®ç°å¯¹
éé¢å对象æ°æ®æºçæ¥è¯¢ææ¯ï¼å¯æ¥è¯¢çæ°æ®æºä»å ³ç³»åæ°æ®åºå»¶ä¼¸å°ä¸è¬æä¹ä¸çéåï¼å¦æ°ç»åå表ï¼ä»¥åXMLãèC# 3.0åæ¯çå å®ç°äº
LINQçè¯è¨ãå¨C# 3.0ä¸ï¼æ们å¯ä»¥ç¨ç±»ä¼¼äºSQLè¯å¥çè¯æ³ä»ä¸ä¸ªæ°æ®æºä¸è½»æ¾å°å¾å°æ»¡è¶³ä¸å®æ¡ä»¶ç对象éåãä¾å¦è¦æ¥æ¾ä¸ä¸ªå符串
æ°ç»namesä¸ææé¿åº¦å¤§äº5çå符串ï¼å°±å¯ä»¥åï¼ var longname = from n in names wheren.Length > 5 select n;è¿æ ·æ们就å¾å°ä¸ä¸ª
æ°çå符æ°ç»longnameï¼å ¶ä¸å å«äºæ们æéè¦çç»æãè¿ç§è¯å¥ç§°ä½æ¥è¯¢è¯å¥ï¼ä¸SQLè¯å¥å¯ä¸çåºå«æ¯C#ä¸çæ¥è¯¢è¯å¥å¾å¾æselectåå¥æ¾
å°æåï¼è¿åèåæäºç±»ä¼¼äºä¸æçé 读顺åºäºï¼ãå次çå°è¿æ ·ä¸ä¸ªè¯å¥ï¼æ们å¯è½ä¼æå¾å¤§çé®ï¼è¿è¿æ¯C#è¯è¨åï¼è¿çç¡®æ¯åä¹è¯æ³è§
åçC#代ç ï¼èä¸ç¼è¯å¨å¯ä»¥è¯å«è¿ç§è¯æ³ãç¶èå®é ä¸ï¼C#ç¼è¯å¨å¹¶ä¸ä¼å¯¹è¿ç§è¯æ³è¿è¡å®é ççç¼è¯ï¼èæ¯å°å ¶ç¿»è¯ä¸ºæ£å¸¸çæ¹æ³è°ç¨ï¼
var longname = names.Where(n => n.
Length > 5).Select(n);ç¶ååè¿è¡è¿ä¸æ¥çç¼è¯ãå¨ä¸é¢çä¾åä¸å·²ç»è¯´æï¼namesæ¯ä¸ä¸ªåæ¾æå符串çæ°ç»ï¼èæ°ç»ç±»å并没æWhereç
æ¹æ³ãçç¡®ï¼Where并énamesçæåæ¹æ³ï¼å¾®è½¯ä¹æ²¡æ对æ°ç»ç±»åè¿è¡ä»»ä½æ¹å¨ãè¿æ¯C# 3.0ä¸å¦å¤ä¸ä¸ªéè¦çæ°ç¹æ§ï¼æ©å±æ¹æ³ãæ©å±æ¹æ³
æ¯å®ä¹å¨å ¶ä»éæç±»ä¸çéææ¹æ³ï¼å ¶ç¬¬ä¸ä¸ªåæ°çç±»åå°±æ¯å¸ææ©å±çç±»åï¼å¹¶ä¸è¿ä¸ªåæ°è¢«å 以this修饰符ãæ©å±æ¹æ³æ¯éæçï¼ä½å¯ä»¥
åè°ç¨è¢«æ©å±ç±»åçå®ä¾æ¹æ³é£æ ·è¿è¡è°ç¨ï¼çèµ·æ¥å¥½åæ¯è¢«æ©å±ç±»åèªå·±çæ¹æ³ä¸æ ·ãè¿å°±ä¸ºè¯è¨å¸¦æ¥äºå¾å¤§ççµæ´»æ§ï¼æ们å¯ä»¥å°ä¸ç»è¿
ä¼¼çåè½å¦ä¸é¢çWhere åSelectçï¼è¿å¨LINQä¸è¢«ç§°ä½âæ åæ¥è¯¢è¡¨è¾¾å¼âï¼å®ä¹å¨ä¸ä¸ªå¤é¨ç±»ä¸ï¼è¿æ ·æ¢æ 须修æ¹ç°æç±»åï¼åå¯ä»¥å°æ°
åè½ç»ç»å¨ä¸èµ·ãå½ç¶ï¼ä¸ºäºåå°é¢å对象çå°è£ æ§ï¼æ©å±æ¹æ³åªè½å¨è¢«æ©å±ç±»åçå ¬å ±æåä¸è¿è¡æä½ï¼å¦æéè¦ä»å é¨å¯¹ç±»åè¿è¡æ¹è¿ï¼
å°±å¿ é¡»æ¹åç°æç±»åç代ç ãå¨Whereæ¹æ³çåæ°å表éï¼æ们ååç°äºä¸ç§å¥æªçè¯æ³ï¼n => n.Length > 5ãè¿å°±æ¯æ们ä¸ææå°è¿ç
Lambda 表达å¼ã微软çå®æ¹è§èä¸ç§°ï¼Lambda 表达å¼æ¯å¿åæ¹æ³çä¸ç§èªç¶è¿åãå æ¤Lambda 表达å¼å ¶å®ä¹æ¯ä¸ç§ç¹æ®çå§æï¼ç±ç¼è¯å¨è´
è´£çæä¸ä¸ªå¿åçå§æç±»åï¼å®æ¥åä¸ä¸ªå符串类åçåæ°nï¼è¿åå¼ä¸ºå¸å°ç±»åï¼è¡¨ç¤ºnçé¿åº¦æ¯å¦å¤§äº5ï¼å ¶ä¸çåæ°ç±»ååè¿åå¼ç±»åé½æ¯
ç±ç¼è¯å¨æ¨æèæ¥çã说å°ç±»åæ¨æï¼è¿è¦è§£éçä¸ç¹å°±æ¯ä¸é¢çè¯å¥ä¸åºç°çæ°å ³é®åvarãä»åºç°çä½ç½®æ¥çï¼varåºè¯¥æ¯ä¸ä¸ªç±»åãç¶è
è¿åä¸æ¯ä¸ä¸ªC#å 建类åï¼ä¹ä¸æ¯CLIæåºçæ°ç±»åï¼å®åªæ¯ä¸ä¸ªâå ä½ç¬¦âï¼å®ç确表示ä¸ä¸ªç±»åï¼ä½å ·ä½æ¯ä»ä¹ç±»åéè¦ç¼è¯å¨å¨ç¼è¯æé´
è¿è¡æ¨æãLamda表达å¼ççæ£æä¹ä¸ä» ä» å¨äºç®åäºå§æçç¼åæ¹å¼ï¼æ´éè¦çæ¯å®æ代ç 表达å¼ä½ç°ä¸ºäºæ°æ®ãæ¢å¥è¯è¯´ï¼Lambda表达å¼ä¸
ä» å¯ä»¥è¢«ç¼è¯ä¸ºä¸æ®µå¯ä»¥æ§è¡ç代ç ï¼ç±»ä¼¼äºå¿åæ¹æ³ï¼ï¼ä¹å¯ä»¥å°å ¶ç¿»è¯ä¸ºä¸ä¸ªæ°æ®ç»æââ表达å¼æ ãèå¦ä½å¤çLambda 表达å¼ï¼æ¯ç±ç¼
è¯å¨æ ¹æ®Lambda表达å¼ç使ç¨æ¹å¼æ¥èªå¨ç¡®å®çãå½æä¸ä¸ªLambda表达å¼èµç»ä¸ä¸ªå ·æå§æç±»åçåãå±æ§æåéæ¶ï¼ç¼è¯å¨åç¼è¯å¿åæ¹æ³
ä¸æ ·å°è¡¨è¾¾å¼ä½ç¿»è¯æä¸æ®µå¯æ§è¡ä»£ç ï¼èå½æä¸ä¸ªL a m b d a 表达å¼èµç»ä¸ä¸ªå ·æExpression<T>ç±»åçåãå±æ§æåéæ¶ï¼ç¼è¯å¨å°±ä¼å°
Lambda表达å¼è§£æ为ä¸ä¸ªè¡¨è¾¾å¼æ ã对äºç¿»è¯ä¸ºä»£ç çLambdaï¼å¯ä»¥åè°ç¨å§æé£æ ·è¿è¡è°ç¨ï¼è对äºç¿»è¯ä¸ºè¡¨è¾¾å¼æ çLambda表达å¼ï¼å°±ä¸
å¯ä»¥äºï¼ä¼å¾å°ä¸ä¸ªç¼è¯é误ãä½è¡¨è¾¾å¼æ åå¨äºä¸ä¸ªç±ç¼è¯å¨çæçæ°æ®ç»æä¸ï¼å æ¤å¯ä»¥å¨è¿è¡æ¶å¯¹å ¶è¿è¡åæçè³ä¿®æ¹ã
é¤äºä¸é¢æå°çä¸äºé大æ¹è¿ä¹å¤ï¼C# 3.0ä¹å¯¹ç»å¾®çè¯æ³è¿è¡äºä¸äºæ¹è¿ï¼ä½¿C#è¯è¨åå¾æ´å ä¼é åå ¨é¢ãå¼å¾è¯´æçæ¯ï¼C# 3.0ç»è¿ç¼è¯
åçæçIL代ç ï¼å®å ¨æ¯åºäº.NET 2.0çï¼C#è¯è¨å·²ç»è¿è¿è·å¨äºä»ææ ççå¹³å°åé¢ãè¿ä¸æ¶æçC#è¯è¨ç¦»CLIå·²ç»è¶æ¥è¶è¿äºï¼ç¼è¯å¨çå·¥
ä½ä¹æå ç¹éèµ·æ¥ãé¦å å¾å¤è¯è¨ç»æï¼å¦æ¥è¯¢è¡¨è¾¾å¼åLambda 表达å¼ï¼é½ä¸æ¯CLIä¸æä¾çç¹æ§ï¼å æ¤éè¦ç¼è¯å¨è¿è¡å¤§éç转è¯å·¥ä½ï¼å ¶
次æ¯è¿äºè¯è¨ç»æ带æ¥ç大éç±»åæ¨æä»»å¡ï¼ä¹é½æ¯é ç¼è¯å¨æ¥å®æçã
C#èµ°å°äº3.0以åï¼å·²ç»å®å ¨ä¸åæ¯å½å¹´é£ä¸ªâç®åâçè¯è¨äºãå®çå¼åè ç§°å ¶ä¸ºâé鬼âï¼èç³ç 满ç®çæ°ç¹æ§ä¹ç确让å¼åè 们ç¼è±ç¼ä¹±
ï¼çè³æå°ææ§ãè¯è¨éææ¥è¯¢çå¼å ¥ï¼ä½¿å¾åä¸æ®µæ¶æå 为å¼åè 们广æ³è®¨è®ºçORMæ¦å¿µå¾å°äºæ´å æ·±å ¥å°ä½ç°ï¼å°¤å ¶æ¯å®ææ¯æçæ°æ®æºä¹
广æ³ï¼è®©ORMç念åå¾å·²ç»ä¸åå¿ è¦äºï¼èä¸äºâ.NETä¸çORMå®ç°âï¼ä¼¼ä¹ä¹æäºå®å ¨ä¸å¿ è¦çæ©å±é¡¹ç®äºãLambda 表达å¼çå¼å ¥ï¼ä½¿å¾C#
å°å¯ä»¥è½»æ¾å°å®æç¹å®é¢åï¼Domain-Specificï¼çå¼åãä¸ä¸ªæåçå¼å人åå¨é¢å¯¹æ°é²äºç©åæ°çå°é¾æ¶ï¼å ´å¥æ¯è¿å¤§äºææ§çã
让é鬼æ¥å¾æ´ççäºå§ï¼
高分求教vc:listbox多行显示
英文资料,看看能不能用上把.
The DrawTextEx function draws formatted text in the specified rectangle.
int DrawTextEx(
HDC hdc, // handle to device context
LPTSTR lpchText, // pointer to string to draw
int cchText, // length of string to draw
LPRECT lprc, // pointer to rectangle coordinates
UINT dwDTFormat, // formatting options
LPDRAWTEXTPARAMS lpDTParams // pointer to structure for more options
);
Parameters
hdc
Identifies the device context to draw in.
lpchText
Points to the string to draw. The string must be null-terminated if the cchText parameter is -1.
cchText
Specifies the length, in characters, of the string specified by the lpchText parameter. If the string is null-terminated, this parameter can be -1 to calculate the length.
lprc
Points to a RECT structure that contains the rectangle, in logical coordinates, in which the text is to be formatted.
dwDTFormat
Specifies formatting options. This parameter can be one or more of these values:
Value Meaning
DT_BOTTOM Justifies the text to the bottom of the rectangle. This value must be combined with DT_SINGLELINE.
DT_CALCRECT Determines the width and height of the rectangle. If there are multiple lines of text, DrawTextEx uses the width of the rectangle pointed to by the lprc parameter and extends the base of the rectangle to bound the last line of text. If there is only one line of text, DrawTextEx modifies the right side of the rectangle so that it bounds the last character in the line. In either case, DrawTextEx returns the height of the formatted text, but does not draw the text.
DT_CENTER Centers text horizontally in the rectangle.
DT_EDITCONTROL Duplicates the text-displaying characteristics of a multiline edit control. Specifically, the average character width is calculated in the same manner as for an edit control, and the function does not display a partially visible last line.
DT_END_ELLIPSIS or DT_PATH_ELLIPSIS Replaces part of the given string with ellipses, if necessary, so that the result fits in the specified rectangle. The given string is not modified unless the DT_MODIFYSTRING flag is specified.You can specify DT_END_ELLIPSIS to replace characters at the end of the string, or DT_PATH_ELLIPSIS to replace characters in the middle of the string. If the string contains backslash (\) characters, DT_PATH_ELLIPSIS preserves as much as possible of the text after the last backslash.
DT_EXPANDTABS Expands tab characters. The default number of characters per tab is eight.
DT_EXTERNALLEADING Includes the font external leading in line height. Normally, external leading is not included in the height of a line of text.
DT_LEFT Aligns text to the left.
DT_MODIFYSTRING Modifies the given string to match the displayed text. This flag has no effect unless the DT_END_ELLIPSIS or DT_PATH_ELLIPSIS flag is specified.
DT_NOCLIP Draws without clipping. DrawTextEx is somewhat faster when DT_NOCLIP is used.
DT_NOPREFIX Turns off processing of prefix characters. Normally, DrawTextEx interprets the ampersand (&) mnemonic-prefix character as a directive to underscore the character that follows, and the double ampersand (&&) mnemonic-prefix characters as a directive to print a single ampersand. By specifying DT_NOPREFIX, this processing is turned off.
DT_RIGHT Aligns text to the right.
DT_RTLREADING Layout in right to left reading order for bi-directional text when the font selected into the hdc is a Hebrew or Arabic font. The default reading order for all text is left to right.
DT_SINGLELINE Displays text on a single line only. Carriage returns and linefeeds do not break the line.
DT_TABSTOP Sets tab stops. The DRAWTEXTPARAMS structure pointed to by the lpDTParams parameter specifies the number of average character widths per tab stop.
DT_TOP Top justifies text (single line only).
DT_VCENTER Centers text vertically (single line only).
DT_WORDBREAK Breaks words. Lines are automatically broken between words if a word extends past the edge of the rectangle specified by the lprc parameter. A carriage return-linefeed sequence also breaks the line.
dwDTParams
Points to a DRAWTEXTPARAMS structure that specifies additional formatting options. This parameter can be NULL.
Return Values
If the function succeeds, the return value is the text height.
If the function fails, the return value is zero.
å ³äºc#ä¸UDPç¼ç¨
//è¿æ¯ä¸ä¸ªæºç ä½ çç
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace UDPClient
{
public partial class frmUdp : Form
{
private UdpClient sendUdpClient;
private UdpClient receiveUpdClient;
public frmUdp()
{
InitializeComponent();
IPAddress[] ips = Dns.GetHostAddresses("");
tbxlocalip.Text = ips[3].ToString();
int port = ;
tbxlocalPort.Text = port.ToString();
tbxSendtoIp.Text = ips[3].ToString();
tbxSendtoport.Text = port.ToString();
}
// æ¥åæ¶æ¯
private void btnReceive_Click(object sender, EventArgs e)
{
// å建æ¥æ¶å¥æ¥å
IPAddress localIp = IPAddress.Parse(tbxlocalip.Text);
IPEndPoint localIpEndPoint = new IPEndPoint(localIp, int.Parse(tbxlocalPort.Text));
receiveUpdClient = new UdpClient(localIpEndPoint);
Thread receiveThread = new Thread(ReceiveMessage);
receiveThread.Start();
}
// æ¥æ¶æ¶æ¯æ¹æ³
private void ReceiveMessage()
{
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
try
{
// å ³éreceiveUdpClientæ¶æ¤æ¶ä¼äº§çå¼å¸¸
byte[] receiveBytes = receiveUpdClient.Receive(ref remoteIpEndPoint);
string message = Encoding.Unicode.GetString(receiveBytes);
// æ¾ç¤ºæ¶æ¯å 容
ShowMessageforView(lstbxMessageView, string.Format("{ 0}[{ 1}]", remoteIpEndPoint, message));
}
catch
{
break;
}
}
}
// å©ç¨å§æåè°æºå¶å®ç°çé¢ä¸æ¶æ¯å 容æ¾ç¤º
delegate void ShowMessageforViewCallBack(ListBox listbox, string text);
private void ShowMessageforView(ListBox listbox, string text)
{
if (listbox.InvokeRequired)
{
ShowMessageforViewCallBack showMessageforViewCallback = ShowMessageforView;
listbox.Invoke(showMessageforViewCallback, new object[] { listbox, text });
}
else
{
lstbxMessageView.Items.Add(text);
lstbxMessageView.SelectedIndex = lstbxMessageView.Items.Count - 1;
lstbxMessageView.ClearSelected();
}
}
private void btnSend_Click(object sender, EventArgs e)
{
if (tbxMessageSend.Text == string.Empty)
{
MessageBox.Show("åéå 容ä¸è½ä¸ºç©º","æ示");
return;
}
// éæ©åé模å¼
if (chkbxAnonymous.Checked == true)
{
// å¿å模å¼(å¥æ¥åç»å®ç端å£ç±ç³»ç»éæºåé )
sendUdpClient = new UdpClient(0);
}
else
{
// å®å模å¼(å¥æ¥åç»å®å°æ¬å°æå®ç端å£)
IPAddress localIp = IPAddress.Parse(tbxlocalip.Text);
IPEndPoint localIpEndPoint = new IPEndPoint(localIp, int.Parse(tbxlocalPort.Text));
sendUdpClient = new UdpClient(localIpEndPoint);
}
Thread sendThread = new Thread(SendMessage);
sendThread.Start(tbxMessageSend.Text);
}
// åéæ¶æ¯æ¹æ³
private void SendMessage(object obj)
{
string message = (string)obj;
byte[] sendbytes = Encoding.Unicode.GetBytes(message);
IPAddress remoteIp = IPAddress.Parse(tbxSendtoIp.Text);
IPEndPoint remoteIpEndPoint = new IPEndPoint(remoteIp, int.Parse(tbxSendtoport.Text));
sendUdpClient.Send(sendbytes, sendbytes.Length, remoteIpEndPoint);
sendUdpClient.Close();
// æ¸ ç©ºåéæ¶æ¯æ¡
ResetMessageText(tbxMessageSend);
}
// éç¨äºåè°æºå¶
// 使ç¨å§æå®ç°è·¨çº¿ç¨çé¢çæä½æ¹å¼
delegate void ResetMessageCallback(TextBox textbox);
private void ResetMessageText(TextBox textbox)
{
// Control.InvokeRequiredå±æ§ä»£è¡¨
// å¦ææ§ä»¶çå¤çä¸è°ç¨çº¿ç¨å¨ä¸å线ç¨ä¸å建çï¼å为true,å¦å为false
if (textbox.InvokeRequired)
{
ResetMessageCallback resetMessagecallback = ResetMessageText;
textbox.Invoke(resetMessagecallback, new object[] { textbox });
}
else
{
textbox.Clear();
textbox.Focus();
}
}
// åæ¢æ¥æ¶
private void btnStop_Click(object sender, EventArgs e)
{
receiveUpdClient.Close();
}
// æ¸ ç©ºæ¥åæ¶æ¯æ¡
private void btnClear_Click(object sender, EventArgs e)
{
this.lstbxMessageView.Items.Clear();
}
}
}
2024-11-30 12:01
2024-11-30 12:01
2024-11-30 11:54
2024-11-30 11:30
2024-11-30 11:00
2024-11-30 10:08