C#的四个基本技巧
1.如果可能尽量使用接口来编程
.NET框架包括类和接口,在编写程序的时候,你可能知道正在用.NET的哪个类。然而,在这种情况下如果你用.NET支持的接口而不是它的类来编程时,代码会变得更加稳定、可用性会更高。请分析下面的代码:
private void LoadList (object [] items, ListBox l)
{
for (int i = 0; i < items.Length;i++)
l.Items.Add (items[i].ToString ());
}
这个函数从一个可为任何对象的数组中加载ListBox,这段代码被限定为只能使用数组。假想过些时候你发现那些对象存在数据库中,或别的集合中。那么你需要修改程序来使用不同的集合类型。如果你用ICollection接口来写那段程序,你就不用修改那段程序了,对于任何实现ICollection接口的类型它都能很好的工作:
private void LoadList (ICollection items,ListBox l)
{
foreach (object o in items)
l.Items.Add (o.ToString ());
}
ICollection被数组和所有System.Collection中的集合实现。此外,多维数组也支持ICollection接口。如果那还不够的话,数据库.NET类同样支持ICollection接口。用接口写的这个函数不用需改就可以才许多中情况下使用。
2. 使用属性代替原始数据
因为属性已经成为语言本身的元素,所以声明数据元素时它的作用域等级没有必要大于private。因为代码本身会把属性看成数据元素,你并没有失去使用简单数据类型的便利性 。相反它会使你的代码更加灵活功能更加强大。属性使你的数据元素封装性更好。属性可以让你使用lazy evaluation来返回数据。lazy evaluation的意思是当用户请求时才计算它的值,而不是一直保留着它。
最后,属性可以是virtual也可以是abstract。你也可以在接口中定义属性。
这里还有维护方面的因素应当注意:尽管操作两者的方法是一样的,但是你把一个数据元素变成属性,那么原先客户端的程序便不能访问服务端的新版本程序了。实际上对于在Web service中你想实现序列化的值你可以把它们变成属性来使用:
private int TheMonth = 0;
[XmlAttribute ("Month")]
public int Month
{
get {
return TheMonth;
}
set {
TheMonth = value;
}
}
简单通过属性就可以使你的所有数据元素私有化。
3. 在Producer/Consumer 的Idiom中使用Delegate
当你生成一个实现producer idiom类的时候,使用deletate来通知consumer。这种方法相对于用接口更加灵活。Delegate是多点传送的,所以不用加额外的代码你就何以支持多用户。相对于用接口这样做可使类之间的耦合性降低。
下面的类处理键盘输入并把它传给所有的registered listeners:
public class KeyboardProcessor
{
private OnGetLine theFunc = null;
public OnGetLine OnGetLineCallback {
get {
return theFunc;
}
set {
theFunc = value;
}
}
public void Run (){
// Read input.
// If there is any listeners, publish:
string s;
do {
s = Console.ReadLine ();
if (s.Length == 0)
break;
if (theFunc != null){
System.Delegate [] funcs =theFunc.GetInvocationList();
foreach (OnGetLine f in funcs) {
try {
f (s);
} catch (Exception e) {
Console.WriteLine
("Caught Exception: {0}", e.Message);
}
}
}
} while (true);
}
任何数目的listeners都可注册到producer,它们所要做的只是提供一个特定的函数:deletate。
4. 注意初始化顺序
C#中对于一些变量声明加入了initializer的概念。它们在构造函数之前被执行,实际上变量在基类的构造函数执行前之前被初始化。
所以,在初始化变量的时候不要用基类中的数据,因为它们还没有被构造。
摘http://www.itpub.net/showthread.php?threadid=611795&pagenumber=
http://community.csdn.net/Expert/topicview.asp?id=5283325
他们设置了哪些标签:
.NET/基础 .net技术 1 a aaaaaaaaaaa C# C#的四个基本技 C#的四个基本技巧 C#技巧 C#四个技巧 CS.Net dd 程序开发 基本技巧 技巧 经典的技巧 网站开发
谁收藏了这个网址:
时间:2007-1-9 9:50:02 | 相关网摘
使用标签:C#,时间:2007-1-9 9:51:07 | 相关网摘
时间:2007-1-9 9:59:51 | 相关网摘
时间:2007-1-9 10:39:08 | 相关网摘
时间:2007-1-9 10:48:40 | 相关网摘
时间:2007-1-9 10:51:18 | 相关网摘
时间:2007-1-9 11:07:24 | 相关网摘
时间:2007-1-9 11:29:14 | 相关网摘
时间:2007-1-9 11:29:56 | 相关网摘
时间:2007-1-9 11:37:39 | 相关网摘
时间:2007-1-9 13:50:40 | 相关网摘
时间:2007-1-9 13:52:21 | 相关网摘
时间:2007-1-9 14:50:56 | 相关网摘
时间:2007-1-9 15:11:06 | 相关网摘
时间:2007-1-9 15:58:48 | 相关网摘
时间:2007-1-9 16:34:26 | 相关网摘
时间:2007-1-9 17:28:47 | 相关网摘
时间:2007-1-9 19:09:21 | 相关网摘
时间:2007-1-9 19:29:09 | 相关网摘
时间:2007-1-9 19:45:39 | 相关网摘
时间:2007-1-10 10:04:47 | 相关网摘
时间:2007-1-10 10:19:11 | 相关网摘
时间:2007-1-10 10:36:39 | 相关网摘
时间:2007-1-10 10:47:37 | 相关网摘
时间:2007-1-10 11:03:29 | 相关网摘
使用标签:C#, 技巧,时间:2007-1-10 11:12:26 | 相关网摘
时间:2007-1-10 11:15:27 | 相关网摘
尽量使用接口来编程
使用标签:C#,时间:2007-1-10 11:22:53 | 相关网摘
时间:2007-1-10 11:36:27 | 相关网摘
时间:2007-1-10 11:54:57 | 相关网摘
时间:2007-1-10 13:06:36 | 相关网摘
时间:2007-1-10 13:09:29 | 相关网摘
时间:2007-1-10 13:14:43 | 相关网摘
时间:2007-1-10 13:59:28 | 相关网摘
时间:2007-1-10 14:03:46 | 相关网摘
时间:2007-1-10 14:16:02 | 相关网摘
时间:2007-1-10 14:39:40 | 相关网摘
时间:2007-1-10 15:00:17 | 相关网摘
时间:2007-1-10 15:24:28 | 相关网摘
时间:2007-1-10 15:38:54 | 相关网摘
时间:2007-1-10 16:08:56 | 相关网摘
时间:2007-1-10 16:31:45 | 相关网摘
时间:2007-1-10 18:50:15 | 相关网摘
时间:2007-1-10 18:53:45 | 相关网摘
时间:2007-1-10 19:21:19 | 相关网摘
时间:2007-1-10 19:38:14 | 相关网摘
时间:2007-1-10 19:59:02 | 相关网摘
时间:2007-1-10 20:04:52 | 相关网摘
时间:2007-1-10 20:18:32 | 相关网摘
时间:2007-1-10 20:43:25 | 相关网摘
时间:2007-1-10 21:39:54 | 相关网摘
时间:2007-1-10 22:13:26 | 相关网摘
时间:2007-1-10 22:30:26 | 相关网摘
时间:2007-1-10 23:01:40 | 相关网摘
使用标签:C#,时间:2007-1-10 23:58:48 | 相关网摘
使用标签:CS.Net,时间:2007-1-11 0:22:03 | 相关网摘
1.如果可能尽量使用接口来编程
2. 使用属性代替原始数据
3. 在Producer/Consumer 的Idiom中使用Delegate
4. 注意初始化顺序
时间:2007-1-11 1:24:42 | 相关网摘
时间:2007-1-11 8:31:39 | 相关网摘
时间:2007-1-11 8:58:21 | 相关网摘
时间:2007-1-11 9:15:02 | 相关网摘
时间:2007-1-11 9:21:14 | 相关网摘
C#的四个基本技巧
时间:2007-1-11 9:30:51 | 相关网摘
时间:2007-1-11 9:57:09 | 相关网摘
时间:2007-1-11 10:03:24 | 相关网摘
时间:2007-1-11 10:13:55 | 相关网摘
时间:2007-1-11 11:22:17 | 相关网摘
时间:2007-1-11 16:46:44 | 相关网摘
时间:2007-1-11 21:26:43 | 相关网摘
时间:2007-1-11 21:52:17 | 相关网摘
时间:2007-1-12 11:19:57 | 相关网摘
时间:2007-1-12 14:27:53 | 相关网摘
时间:2007-1-12 22:07:39 | 相关网摘
时间:2007-1-13 9:23:30 | 相关网摘
时间:2007-1-13 10:11:02 | 相关网摘
时间:2007-1-13 14:39:11 | 相关网摘
时间:2007-1-13 16:41:03 | 相关网摘
时间:2007-1-13 17:48:14 | 相关网摘
时间:2007-1-13 21:44:11 | 相关网摘
时间:2007-1-13 22:53:00 | 相关网摘
时间:2007-1-14 1:00:46 | 相关网摘
时间:2007-1-14 22:03:55 | 相关网摘
C#的四个基本技巧
时间:2007-1-14 22:16:58 | 相关网摘
时间:2007-1-15 10:03:07 | 相关网摘
时间:2007-1-15 10:24:52 | 相关网摘
时间:2007-1-16 8:47:18 | 相关网摘
时间:2007-1-16 9:18:48 | 相关网摘
时间:2007-1-16 9:35:20 | 相关网摘
时间:2007-1-16 9:44:01 | 相关网摘
时间:2007-1-16 10:05:33 | 相关网摘
时间:2007-1-16 11:12:27 | 相关网摘
1.如果可能尽量使用接口来编程
2. 使用属性代替原始数据
3. 在Producer/Consumer 的Idiom中使用Delegate
4. 注意初始化顺序