C#编程技巧七条

开发 后端
本文总结了7条C#编程技巧,供大家参考。

C#编程技巧之一、最小化窗口

点击“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);  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

C#编程技巧之二、如何让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);  
}  
}  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

你可以将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();  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

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

C#编程技巧之三、将图片保存到一个XML文件

WinForm的资源文件中,将PictureBox的Image属性等非文字内容都转变成文本保存,这是通过序列化(Serialization)实现的,

例子:

using System.Runtime.Serialization.Formatters.Soap;  
Stream stream = new FileStream("E:\\Image.XML",FileMode.Create,FileAccess.Write,FileShare.None);  
SoapFormatter f = new SoapFormatter();  
Image img = Image.FromFile("E:\\Image.bmp");  
f.Serialize(stream,img);  
stream.Close();  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

C#编程技巧之四、屏蔽CTRL-V

在WinForm中的TextBox控件没有办法屏蔽CTRL-V的剪贴板粘贴动作,如果需要一个输入框,但是不希望用户粘贴剪贴板的内容,可以改用RichTextBox控件,并且在KeyDown中屏蔽掉CTRL-V键,例子:

private void richTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)  
{  
if(e.Control && e.KeyCode==Keys.V)  
e.Handled = true;  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

C#编程技巧之五、判断文件或文件夹是否存在

使用System.IO.File,要检查一个文件是否存在非常简单:

bool exist = System.IO.File.Exists(fileName); 
  • 1.

如果需要判断目录(文件夹)是否存在,可以使用System.IO.Directory:

bool exist = System.IO.Directory.Exists(folderName); 
  • 1.

C#编程技巧之六、使用delegate类型设计自定义事件

在C#编程中,除了Method和Property,任何Class都可以有自己的事件(Event)。定义和使用自定义事件的步骤如下:

(1)在Class之外定义一个delegate类型,用于确定事件程序的接口

(2)在Class内部,声明一个public event变量,类型为上一步骤定义的delegate类型

(3)在某个Method或者Property内部某处,触发事件

(4)Client程序中使用+=操作符指定事件处理程序

例子: // 定义Delegate类型,约束事件程序的参数

public delegate void MyEventHandler(object sender, long lineNumber) ;  
public class DataImports  
{  
// 定义新事件NewLineRead  
public event MyEventHandler NewLineRead ;  
public void ImportData()  
{  
long i = 0 ; // 事件参数  
while()  
{  
i++ ;  
// 触发事件  
if( NewLineRead != null ) NewLineRead(this, i);  
//...  
}  
//...  
}  
//...  
}  
// 以下为Client代码  
private void CallMethod()  
{  
// 声明Class变量,不需要WithEvents  
private DataImports _da = null;  
// 指定事件处理程序  
_da.NewLineRead += new MyEventHandler(this.DA_EnterNewLine) ;  
 
// 调用Class方法,途中会触发事件  
_da.ImportData();  
}  
// 事件处理程序  
private void DA_EnterNewLine(object sender, long lineNumber)  
{  
// ...  
}  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.

C#编程技巧之七、IP与主机名解析

使用System.Net可以实现与Ping命令行类似的IP解析功能,例如将主机名解析为IP或者反过来:

private string GetHostNameByIP(string ipAddress)  
{  
IPHostEntry hostInfo = Dns.GetHostByAddress(ipAddress);  
return hostInfo.HostName;  
}  
private string GetIPByHostName(string hostName)  
{  
System.Net.IPHostEntry hostInfo = Dns.GetHostByName(hostName);  
return hostInfo.AddressList[0].ToString();  
}  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

【编辑推荐】

  1. 介绍C#中的值类型
  2. C#连接Access、SQL Server数据库
  3. 谈谈C#日期格式化和数字格式化的实现
  4. ASP.NET初探:什么是ASP.NET
  5. 介绍C#调用API的问题
责任编辑:book05 来源: csdn
相关推荐

2024-04-17 08:05:18

C#并发设计

2022-11-02 10:31:01

IT创IT领导者

2021-10-29 05:52:01

零信任网络安全网络攻击

2023-07-29 11:40:25

GitForeman

2010-09-08 15:07:23

2013-05-28 14:18:04

2014-02-19 10:44:55

BYOD建议

2018-05-23 20:56:49

开发原因原则

2021-08-03 14:17:47

Kubernetes容器安全

2009-06-09 22:14:17

JavaScript准则

2017-03-02 07:36:40

科技新闻早报

2023-01-20 08:56:04

CIOIT领导

2012-09-25 11:28:38

C#网络协议UDP

2009-08-12 14:01:17

C# Excel编程技

2021-12-15 12:35:51

C语言编程内存

2022-06-15 15:30:29

Linux新用户建议

2020-05-11 07:55:53

AWS系统

2010-09-17 14:24:10

2025-04-10 08:00:00

CIO风险管理IT战略

2010-01-21 11:38:35

点赞
收藏

51CTO技术栈公众号