首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 数据库 第二书店 程序员

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=





他们设置了哪些标签:


.NET/基础 .net技术 1 a aaaaaaaaaaa C# C#的四个基本技 C#的四个基本技巧 C#技巧 C#四个技巧 CS.Net dd 程序开发 基本技巧 技巧 经典的技巧 网站开发

谁收藏了这个网址:


yanglilibaobao收录

时间:2007-1-9 9:50:02 | 相关网摘

badao收录

使用标签:C#,时间:2007-1-9 9:51:07 | 相关网摘

ojekleen9收录

时间:2007-1-9 9:59:51 | 相关网摘

bill1315收录

时间:2007-1-9 10:39:08 | 相关网摘

fengwuxie收录

时间:2007-1-9 10:48:40 | 相关网摘

liujia_0421收录

时间:2007-1-9 10:51:18 | 相关网摘

ydsunny收录

时间:2007-1-9 11:07:24 | 相关网摘

bess19820628收录

时间:2007-1-9 11:29:14 | 相关网摘

tianyi_hsy收录

时间:2007-1-9 11:29:56 | 相关网摘

jxf_ioriyagami收录

时间:2007-1-9 11:37:39 | 相关网摘

hanlang收录

时间:2007-1-9 13:50:40 | 相关网摘

wubaojie收录

时间:2007-1-9 13:52:21 | 相关网摘

swj8510收录

时间:2007-1-9 14:50:56 | 相关网摘

mldswt收录

时间:2007-1-9 15:11:06 | 相关网摘

softfwind收录

使用标签:C#的四个基本技巧,时间:2007-1-9 15:53:27 | 相关网摘

C#的四个基本技巧

josiesun收录

时间:2007-1-9 15:58:48 | 相关网摘

xfj_aiyu收录

时间:2007-1-9 16:34:26 | 相关网摘

sunssuns收录

时间:2007-1-9 17:28:47 | 相关网摘

Msconfig_001收录

时间:2007-1-9 19:09:21 | 相关网摘

killer000777收录

时间:2007-1-9 19:29:09 | 相关网摘

ddr2006收录

时间:2007-1-9 19:45:39 | 相关网摘

Reasoncool收录

时间:2007-1-10 10:04:47 | 相关网摘

c_hua6280收录

使用标签:.NET技术,时间:2007-1-10 10:05:04 | 相关网摘

hunhun02收录

时间:2007-1-10 10:19:11 | 相关网摘

yahui0313收录

时间:2007-1-10 10:36:39 | 相关网摘

FlyBird2004收录

时间:2007-1-10 10:47:37 | 相关网摘

vins698收录

时间:2007-1-10 11:03:29 | 相关网摘

nemolor收录

使用标签:C#, 技巧,时间:2007-1-10 11:12:26 | 相关网摘

windyman518收录

时间:2007-1-10 11:15:27 | 相关网摘

尽量使用接口来编程

HeroHxw收录

使用标签:C#,时间:2007-1-10 11:22:53 | 相关网摘

jjwbs收录

时间:2007-1-10 11:36:27 | 相关网摘

gshadows收录

时间:2007-1-10 11:54:57 | 相关网摘

ElvisYH收录

时间:2007-1-10 13:06:36 | 相关网摘

ahcsdn收录

时间:2007-1-10 13:09:29 | 相关网摘

wwwlike收录

时间:2007-1-10 13:14:43 | 相关网摘

pphuyidao收录

时间:2007-1-10 13:59:28 | 相关网摘

mao924收录

时间:2007-1-10 14:03:46 | 相关网摘

yangao收录

使用标签:.NET/基础,时间:2007-1-10 14:12:06 | 相关网摘

cspyb收录

时间:2007-1-10 14:16:02 | 相关网摘

fengshl收录

使用标签:C#的四个基本技巧,时间:2007-1-10 14:18:29 | 相关网摘

C#的四个基本技巧

liucan收录

时间:2007-1-10 14:39:40 | 相关网摘

ap0106204收录

时间:2007-1-10 15:00:17 | 相关网摘

shiliangdong收录

时间:2007-1-10 15:24:28 | 相关网摘

songpengyue收录

时间:2007-1-10 15:38:54 | 相关网摘

xiyou收录

时间:2007-1-10 16:08:56 | 相关网摘

RunUpwind收录

时间:2007-1-10 16:31:45 | 相关网摘

bl7009收录

使用标签:C#四个技巧,时间:2007-1-10 17:13:31 | 相关网摘

C#四个技巧

max143收录

时间:2007-1-10 18:50:15 | 相关网摘

zzhdavid收录

时间:2007-1-10 18:53:45 | 相关网摘

coffeeant收录

时间:2007-1-10 19:21:19 | 相关网摘

yitian130收录

时间:2007-1-10 19:38:14 | 相关网摘

netwenchao收录

使用标签:C#技巧,时间:2007-1-10 19:49:02 | 相关网摘

byfq收录

时间:2007-1-10 19:59:02 | 相关网摘

zhouzidane46收录

时间:2007-1-10 20:04:52 | 相关网摘

cubell收录

时间:2007-1-10 20:18:32 | 相关网摘

net_live收录

时间:2007-1-10 20:43:25 | 相关网摘

wsyois收录

时间:2007-1-10 21:39:54 | 相关网摘

puzih收录

时间:2007-1-10 22:13:26 | 相关网摘

lishuiqing收录

时间:2007-1-10 22:30:26 | 相关网摘

mars_feng收录

时间:2007-1-10 23:01:40 | 相关网摘

RoadTeam收录

使用标签:C#,时间:2007-1-10 23:58:48 | 相关网摘

CnEve收录

使用标签:CS.Net,时间:2007-1-11 0:22:03 | 相关网摘

1.如果可能尽量使用接口来编程
2. 使用属性代替原始数据
3. 在Producer/Consumer 的Idiom中使用Delegate
4. 注意初始化顺序

duber收录

时间:2007-1-11 1:24:42 | 相关网摘

Influence收录

时间:2007-1-11 8:31:39 | 相关网摘

gangzichh收录

时间:2007-1-11 8:58:21 | 相关网摘

jackie2004收录

使用标签:C#的四个基本技巧,时间:2007-1-11 9:09:20 | 相关网摘

C#的四个基本技巧

boat2002w收录

时间:2007-1-11 9:15:02 | 相关网摘

shixiangzb007收录

时间:2007-1-11 9:21:14 | 相关网摘

C#的四个基本技巧

wxssaa收录

时间:2007-1-11 9:30:51 | 相关网摘

xiaohutushen收录

时间:2007-1-11 9:57:09 | 相关网摘

suceng1984收录

时间:2007-1-11 10:03:24 | 相关网摘

sep229收录

时间:2007-1-11 10:13:55 | 相关网摘

grapefruitli收录

时间:2007-1-11 11:22:17 | 相关网摘

junge847收录

使用标签:网站开发,时间:2007-1-11 11:50:22 | 相关网摘

xunshine收录

时间:2007-1-11 16:46:44 | 相关网摘

e6734757收录

时间:2007-1-11 21:26:43 | 相关网摘

hy_lihuan收录

时间:2007-1-11 21:52:17 | 相关网摘

hisokahl收录

时间:2007-1-12 11:19:57 | 相关网摘

daviszr_1979收录

时间:2007-1-12 14:27:53 | 相关网摘

xierhub收录

时间:2007-1-12 22:07:39 | 相关网摘

IceQuake收录

时间:2007-1-13 9:23:30 | 相关网摘

superleung收录

时间:2007-1-13 10:11:02 | 相关网摘

yao123收录

时间:2007-1-13 14:39:11 | 相关网摘

kukou收录

时间:2007-1-13 16:41:03 | 相关网摘

fwso2008收录

时间:2007-1-13 17:48:14 | 相关网摘

abcdman收录

时间:2007-1-13 21:44:11 | 相关网摘

billmo1986收录

时间:2007-1-13 22:53:00 | 相关网摘

showlie收录

时间:2007-1-14 1:00:46 | 相关网摘

tianxingzh收录

使用标签:C#的四个基本技巧,时间:2007-1-14 10:27:00 | 相关网摘

C#的四个基本技巧

SoftProgress收录

时间:2007-1-14 22:03:55 | 相关网摘

C#的四个基本技巧

wspnet收录

时间:2007-1-14 22:16:58 | 相关网摘

cqliuhang2006收录

时间:2007-1-15 10:03:07 | 相关网摘

ayan0517收录

时间:2007-1-15 10:24:52 | 相关网摘

gdggs168998收录

使用标签:C#的四个基本技巧,时间:2007-1-15 13:56:49 | 相关网摘

C#的四个基本技巧

zhangyz215收录

时间:2007-1-16 8:47:18 | 相关网摘

wsjeep收录

时间:2007-1-16 9:18:48 | 相关网摘

renyanbinnet收录

时间:2007-1-16 9:35:20 | 相关网摘

nancun收录

时间:2007-1-16 9:44:01 | 相关网摘

c11_11_11收录

时间:2007-1-16 10:05:33 | 相关网摘

landy_shasha收录

时间:2007-1-16 11:12:27 | 相关网摘

1.如果可能尽量使用接口来编程
2. 使用属性代替原始数据
3. 在Producer/Consumer 的Idiom中使用Delegate
4. 注意初始化顺序

网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
Copyright © 2000-2008, CSDN.NET, All Rights Reserved