﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-快快乐乐每一天-文章分类-visual C#.net</title><link>http://www.cnitblog.com/shxu/category/407.html</link><description /><language>zh-cn</language><lastBuildDate>Sun, 02 Oct 2011 05:21:43 GMT</lastBuildDate><pubDate>Sun, 02 Oct 2011 05:21:43 GMT</pubDate><ttl>60</ttl><item><title>C#的四个基本技巧</title><link>http://www.cnitblog.com/shxu/articles/1075.html</link><dc:creator>shxu</dc:creator><author>shxu</author><pubDate>Wed, 20 Jul 2005 00:04:00 GMT</pubDate><guid>http://www.cnitblog.com/shxu/articles/1075.html</guid><wfw:comment>http://www.cnitblog.com/shxu/comments/1075.html</wfw:comment><comments>http://www.cnitblog.com/shxu/articles/1075.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/shxu/comments/commentRss/1075.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/shxu/services/trackbacks/1075.html</trackback:ping><description><![CDATA[<P>1．如果可能尽量使用接口来编程</P>
<P>　　.NET框架包括类和接口，在编写程序的时候，你可能知道正在用.NET的哪个类。然而，在这种情况下如果你用.NET支持的接口而不是它的类来编程时，代码会变得更加稳定、可用性会更高。请分析下面的代码：<BR>private void LoadList (object [] items, ListBox l) <BR>{<BR>　for (int i = 0; i &lt; items.Length;i++)<BR>　　l.Items.Add (items[i].ToString ());<BR>} </P>
<P>　　这个函数从一个可为任何对象的数组中加载ListBox，这段代码被限定为只能使用数组。假想过些时候你发现那些对象存在数据库中，或别的集合中。那么你需要修改程序来使用不同的集合类型。如果你用ICollection接口来写那段程序，你就不用修改那段程序了，对于任何实现ICollection接口的类型它都能很好的工作:</P>
<P>private void LoadList (ICollection items,ListBox l) <BR>{<BR>　　foreach (object o in items)<BR>　　l.Items.Add (o.ToString ());<BR>} </P>
<P>　　ICollection被数组和所有System.Collection中的集合实现。此外，多维数组也支持ICollection接口。如果那还不够的话，数据库.NET类同样支持ICollection接口。用接口写的这个函数不用需改就可以才许多中情况下使用。</P>
<P>　　2. 使用属性代替原始数据</P>
<P>　　因为属性已经成为语言本身的元素，所以声明数据元素时它的作用域等级没有必要大于private。因为代码本身会把属性看成数据元素，你并没有失去使用简单数据类型的便利性 。相反它会使你的代码更加灵活功能更加强大。属性使你的数据元素封装性更好。属性可以让你使用lazy evaluation来返回数据。lazy evaluation的意思是当用户请求时才计算它的值，而不是一直保留着它。</P>
<P>　　最后，属性可以是virtual也可以是abstract。你也可以在接口中定义属性。</P>
<P>　　这里还有维护方面的因素应当注意：尽管操作两者的方法是一样的，但是你把一个数据元素变成属性，那么原先客户端的程序便不能访问服务端的新版本程序了。实际上对于在Web service中你想实现序列化的值你可以把它们变成属性来使用：</P>
<P>private int TheMonth = 0;</P>
<P>[XmlAttribute ("Month")]<BR>public int Month <BR>{<BR>　get {<BR>　　return TheMonth;<BR>　}<BR>　set {<BR>　　TheMonth = value;<BR>　}<BR>} </P>
<P>　　简单通过属性就可以使你的所有数据元素私有化。</P>
<P>　　3. 在Producer/Consumer 的Idiom中使用Delegate</P>
<P>　　当你生成一个实现producer idiom类的时候，使用deletate来通知consumer。这种方法相对于用接口更加灵活。Delegate是多点传送的，所以不用加额外的代码你就何以支持多用户。相对于用接口这样做可使类之间的耦合性降低。</P>
<P>　　下面的类处理键盘输入并把它传给所有的registered listeners：</P>
<P>public class KeyboardProcessor<BR>{<BR>private OnGetLine theFunc = null;</P>
<P>public OnGetLine OnGetLineCallback {<BR>　get {<BR>　　return theFunc;<BR>　}<BR>　set {<BR>　　theFunc = value;<BR>　}<BR>}</P>
<P>public void Run (){<BR>// Read input.<BR>// If there is any listeners, publish:<BR>string s;<BR>do {<BR>　s = Console.ReadLine ();<BR>　if (s.Length == 0)<BR>　　break;<BR>　if (theFunc != null){<BR>　　System.Delegate [] funcs =theFunc.GetInvocationList();<BR>　　foreach (OnGetLine f in funcs) {<BR>　　　try {<BR>　　　　f (s);<BR>　　　} catch (Exception e) {<BR>　　　　Console.WriteLine <BR>　　　　("Caught Exception: {0}", e.Message);<BR>　　　}<BR>　　}<BR>　}<BR>} <BR>while (true);<BR>} </P>
<P>　　任何数目的listeners都可注册到producer，它们所要做的只是提供一个特定的函数：deletate。</P>
<P>　　4. 注意初始化顺序</P>
<P>　　C＃中对于一些变量声明加入了initializer的概念。它们在构造函数之前被执行，实际上变量在基类的构造函数执行前之前被初始化。</P>
<P>　　所以，在初始化变量的时候不要用基类中的数据，因为它们还没有被构造。</P><img src ="http://www.cnitblog.com/shxu/aggbug/1075.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/shxu/" target="_blank">shxu</a> 2005-07-20 08:04 <a href="http://www.cnitblog.com/shxu/articles/1075.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>