首页 资讯应用与安全Web开发数据库编程语言开源服务管理信息化科技市场 技术论坛
[栏目导航]>>[主页]>>[编程语言]>>[C、C++、C#]>>[阅读信息]
 [关闭
C#net编程的七个小技巧
发布时间:2008-09-06 10:14:43   点评:0   人气:29 

   一、最小化窗口

  点击“X”或“Alt+F4”时,最小化窗口,如:

protected override void WndProc(ref Message m)

{
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;
if (m.Msg == WM_SYSCOMMAND && (int) m.WParam == SC_CLOSE)
{
// User clicked close button
this.WindowState = FormWindowState.Minimized;
return;
}
base.WndProc(ref m);
}

   二、如何让Foreach 循环运行的更快

  foreach是一个对集合中的元素进行简单的枚举及处理的现成语句,用法如下例所示:

using System;
using System.Collections;
namespace LoopTest
{
class Class1
{
static void Main(string[] args)
{
// create an ArrayList of strings
ArrayList array = new ArrayList();
array.Add("Marty");
array.Add("Bill");
array.Add("George");
// print the value of every item
foreach (string item in array)
{
Console.WriteLine(item);
}
}
}

  你可以将foreach语句用在每个实现了Ienumerable接口的集合里。如果想了解更多foreach的用法,你可以查看。NET Framework SDK文档中的 C# Language Specification.

  在编译的时候,C#编辑器会对每一个foreach 区域进行转换。

IEnumerator enumerator = array.GetEnumerator();
try
{
string item;
while (enumerator.MoveNext())
{
item = (string) enumerator.Current;
Console.WriteLine(item);
}
}
finally
{
IDisposable d = enumerator as IDisposable;
if (d != null) d.Dispose();
}

  这说明在后台,foreach的管理会给你的程序带来一些增加系统开销的额外代码。

 [关闭]
C、C++、C#    上一条:  C#应用:读取指定盘符的硬盘序列号
   下一条:  C/C++中的整型常识
点评(0) 表扬 赞成 补充 纠正 否定 提醒 举报     笔名:
[提交]