关于.net的一点常识

来源:互联网 发布:二级vb书籍 编辑:程序博客网 时间:2024/05/16 18:19

1. 获取“我的文档”等一些系统文件夹路径
MessageBox.Show(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
2. 确定当前运行的系统
OperatingSystem os   = Environment.OSVersion;
MessageBox.Show(os.Version.ToString());
MessageBox.Show(os.Platform.ToString());
3. 模拟触发buttonclick事件
button1.PerformClick();
4. 创建一个可改变大小没有标题栏的窗体
form1.Test = string.Empty;
form1.ControlBox = false;
5. 如何在.NET的Windows窗体上启用XP主题集?(How to use XP Themes with Windows Forms using the .NET?)
确认你的控件中FlatStyle属性已经修改为System,再修改Main方法。
static void Main()
{
Application.EnableVisualStyles();
Application.DoEvents();
Application.Run(new Form1());
}
6. 为一个窗体设置一个默认按钮
    form1.AcceptButton = button1;
7. 为一个窗体设置一个取消按钮
    form1.CancelButton = button1;
8. 用现有可用字体绑定到ComboBox控件
comboBox1.Items.AddRange(FontFamily.Families);
9. 清理语句
private bool Disposed;
private void Disposed()
{
if(Disposed)
   return;

Disposed = true;
GC.SuppressFinalize(this);
}
10. 声明索引属性
   public String this[String name]
{
   get
{
retrun (String) lookuptable[name];
}
}
11. 访问索引属性
   String s = Request.QueryString["Name"];
   String value = Request.Cookies["Key"];
12. 声明简单属性
   public string name
{
   get
{
....
                   return......;
}
   set
{
.......=value;
}
}
13.   声明和使用枚举
// Declare the Enumeration
   public enum MessageSize
{
   Small = 0,
   Medium =1,
   Large = 2
}
// Creat a Field or Property
   public MessageSize msgsize;
// Assign to the property using the Enumeration values
   msgsize = Small;
14. 枚举集合
   foreach (String s in coll) {
}
15. 声明和使用方法
   // Declare a void return function
   void voidfunction() {
   .......
}
// Declare a function tha retruns a value
   String stirngfunction() {
      .............
         return (String) val;
}
// Declare a function that takes and returns values
   String parmfunction(String a , String b) {
            .........
             return (String) (a + b);
}
// Use the Functions
voidfunction();
String s1 = stringfunction();
String s2 = parmfunction("Hello","World!");
16. 自定义特性
// Stand-alone attribute
[STAThread]
// Attribute with parameters
[DllImport("ADVAPI32.DLL"]
// Attribute with named parameters
[DllImport("KERNEL32.DLL",CharSet = CharSet.Auto)]
17. 数组
    String[] a = new String[3];
    a[0] ="1";
    a[1] ="2";
    a[2] = "3";
String[][] a = new String[3][3];
    a[0][0] = "1";
    a[1][0] = "2";
    a[2][0] = "3";
18. 初始化
    String s= "Hello , World!";
    int i = 2;
    double[] a = {3.00,4.00,5.00};
19. If 语句
    if(Request .QueryString != null) {
      ......
}
20. Case语句
    switch (FirstName) {
   case "John" :
            ......
             break;
   case"aul" :
             ..........
         break;
   case"Ringo" :
         .........
         break;
   dafault :
         ...........
         break;
}
21. For 循环
   for ( int i = 0; i < 3; i++)
a(i) = "test";
22. While 循环
   int i = 0;
   while( i < 3)
Console.WriteLine(i.ToString());
i += 1;
}
23. 异常处理
   try{
         // Code that throws exceptions
} catch(OverflowException e) {
         //Catch a specific exception
} catch(Exception e) {
         // Catch the generic exceptions
} finally {
         // Execute some cleanup code
}
24. 字符串处理
// Using Strings
String s1;
String s2 = "hello":
s2 += "World";
s1 = s2 + "!!!!!";
//Using StringBuilder class for performance
StringBuilder s3 = new StringBuilder();
s3.Append("hello");
s3.Append(" world");
s3.Append(" !!!!");
25. 事件处理程序委托
       void MyButton_Click(Object sender, EventArgs) {
             ..............
}
26. 声明事件
      // Creat a public event
      public event EventHandler MyEvent;
      // Creat a method for firing the event
      protected void OnMyEvent (EventArgs e) {
                MyEvent (this , e);
}
27. 向事件添加事件处理程序或从事件移除事件处理程序
   Control .Change += new EventHandler(this.ChangeEventHandler);
   Control.Change   -=   newEventHandler(this.ChangeEventHandler):
28. 强制类型转换
   MyObject obj = (MyObject) Session["Some Value"];
   IMyObject iobj = obj;
29.转换
    int i = 3;
    String s = i.ToString();
    double d = Double.Parse(s);
30. 带继承的类定义
    using System;
namespace MySpace {
public class Foo : Bar {
   int x;
    public Foo() { x = 4;}
    public void Add(int x) { this.x +=x;}
    override public int GetNum() { return x; }
}
}
// csc /out : librarycs.dll / t :library
// library.cs
31. 实现接口
    public class MyClall : IEnumerable {
...............
          IEnumerator IEnumerable.GetEnumerator () {
                               ...............
                  }
}
32. 带 Main 方法的类定义
using System;
public class ConsoleCS {