C#初学者请进:分享一些C#.Net培训源码
作者:C/S原创  发布日期:2011/03/31 14:23:29
  C#初学者请进:分享一些C#.Net培训源码

C#初学者请进:分享一些C#.Net培训源码



C#初学者请进:分享一些C#.Net培训源码



Console.WriteLine("请按回车键继续...");

Console.Read();

DemoCase CASE = new DemoCase();

CASE.TestDelegate();//测试事件
CASE.TestDelegateAdvanced();//测试事件高级篇
CASE.TestCustomAttribute();////测试自定义属性
CASE.TestCollection();
CASE.TestConverter();
CASE.TestCreateCollection();
CASE.TestEnumerator();
CASE.TestEqual();//对象相等
CASE.TestEqual1();//对象相等
CASE.TestGenericType();//泛型
CASE.TestList();
CASE.TestOperator();
CASE.TestThreadPool();//多线程
CASE.TestXML();//XML


// 来源:www.CSFramework.com, C/S结构框架学习网



DemoCase.cs


public class DemoCase
{
   
   public void TestXML()
   {
      XmlDocument xml = new XmlDocument();
      xml.Load(Application.StartupPath @"\demo.xml");
      XmlNode node = xml.SelectSingleNode("root/customers");
      Console.WriteLine(node == null ? "没有找到customers" : node.Value);
      
      XmlNode node_sub = xml.SelectSingleNode("root/customers/Customer[@name='GG']");
      Console.WriteLine(node == null ? "没有找到GG" : node_sub.Value);
      
      Console.ReadLine();
   }
   
   
   public void TestDynamicArray()
   {
      int[] numbers; // declare numbers as an int array of any size
      
      numbers = new int[20];  // now it's a 20-element array
      
      string[,] names = new string[2, 3];//2,2維行3列
      foreach (string a in names)
      {
         Console.WriteLine(a.ToString());
      }
      
      int[, ,] cube = new int[2, 2, 6];//3維,維度=[x(2),y(2),z(6)]
      
      int[,][] money = new int[2, 2][];//數組的數組
      
      Array arr = numbers;
      foreach (int b in arr)
      Console.WriteLine(b.ToString());
      
      Console.ReadLine();
   }
   
   public void Test()
   {
      int[] numbers = new int[20];
      Array arr = numbers;//類型轉換
      foreach (int b in arr)
      Console.WriteLine(b.ToString());
      Console.ReadLine();
      
      IEnumerator e = numbers.GetEnumerator();
      
      Console.ReadLine();
   }
   
   public void TestDelegate()
   {
      TextBox _TextBox = new TextBox();
      
      //手動綁定click事件
      _TextBox.TextChanged = new EventHandler(OnTextChange);
      
      _TextBox.Text = "abc";
      _TextBox.Text = "111";
      
      Console.ReadLine();
   }
   
   public void TestDelegateAdvanced()
   {
      Customer customer = new Customer("jonny");
      customer.CustomEvent = new MyDelegate(customer_CustomEvent);
      customer.Name = "修改名称,将触发CustomEvent事件";
      Console.ReadLine();
   }
   
   void customer_CustomEvent(object sender, string currentValue)
   {
      Console.WriteLine(currentValue);
   }
   
   
   //處理Click事件
   private void OnTextChange(object sender, EventArgs e)
   {
      Console.WriteLine("修改了数据...");
   }
   
   public void TestEnumerator()
   {
      Customer a = new Customer("A");
      Customer b = new Customer("B");
      Customer[] customers = new Customer[] { a, b }; //創建數組
      IEnumerator e = customers.GetEnumerator();//獲取枚舉器
      while (e.MoveNext())
      Console.WriteLine((e.Current as Customer).Name);
      Console.ReadLine();
   }
   
   public void TestCollection()
   {
      Customer a = new Customer("A");
      Customer b = new Customer("B");
      Customer[] customers = new Customer[] { a, b }; //創建數組
      Console.WriteLine("创建数组,对象:A,B");
      ICollection coll = customers;
      IEnumerator e = coll.GetEnumerator();//獲得集合的列舉器
      //foreach語法
      foreach (Customer c in coll) Console.WriteLine(c.Name);
      Console.ReadLine();
   }
   
   public void TestList()
   {
      IList list = new ArrayList();
      list.Add(new Customer("JONNY SUN"));
      list.Add(new Customer("SHEERY LING"));
      foreach (Customer c in list)
      {
         Console.WriteLine(c.Name);
         Console.WriteLine("ToString():" c.ToString());
      }
      Console.ReadLine();
   }
   
   public void TestOperator()
   {
      //操作符,显式转换,
      object men = new Customer("JONNY SUN");
      
      //is操作符
      if (men is Customer)
      Console.WriteLine("is操作符应用" (men as Customer).Name);
      
      //獲取堆棧中值類型需要的長度
      int a = sizeof(decimal);
      Console.WriteLine("int类型占字节数:" a.ToString());
      
      //typeof獲取類的類型(Delphi內稱為類類型)
      Type t = typeof(Customer);
      Console.WriteLine("typeof方法:" t.FullName);
      
      Console.WriteLine(typeof(Customer).FullName);
      
      Console.ReadLine();
   }
   
   public void TestConverter()
   {
      object o = new Customer("Jonny Sun");
      Console.WriteLine("顯式轉換");
      Customer men0 = o as Customer; //顯式轉換
      
      Console.WriteLine("強製轉換");
      Customer men1 = (Customer)o;//強製轉換
      
      Console.ReadLine();
   }
   
   public void TestEqual()
   {
      Customer a = new Customer("A");
      Customer b = new Customer("A");
      
      Console.WriteLine("Equals()方法使用");
      bool equal = a.Equals(b); //調用Equals方法比較
      
      if (equal)
      Console.WriteLine("兩對象相等");
      else
      Console.WriteLine("兩對象不相等");
      
      Console.ReadLine();
   }
   
   public void TestEqual1()
   {
      Customer a = new Customer("A");
      Customer b = new Customer("A");
      
      Console.WriteLine("對象的引用地址比較");
      bool equal = a == b; //對象的引用地址比較
      
      if (equal)
      Console.WriteLine("兩對象地址相同");
      else
      Console.WriteLine("兩對象地址不相同");
      
      Console.ReadLine();
   }
   
   public void TestGenericType()
   {
      Console.WriteLine("泛型使用");
      
      Console.WriteLine("定義一個處理Customer類型的泛型類");
      GenericClass<Customer> test = new GenericClass<Customer>();
      test.AddObject(new Customer("A"));
      test.AddObject(new Customer("B"));
      Console.WriteLine(test.ObjectCount.ToString());
      Console.ReadLine();
      
      Console.WriteLine("定義一個處理數字類型的泛型類");
      GenericClass<int> t = new GenericClass<int>();
      t.AddObject(1);
      t.AddObject(2);
      Console.ReadLine();
   }
   
   public void TestCreateCollection()
   {
      Console.WriteLine("常用集合类型,和其它特别数据结构");
      ArrayList al = new ArrayList();
      Queue q = new Queue();
      Stack s = new Stack();
      SortedList ss = new SortedList();
      Hashtable h = new Hashtable();
      Dictionary<string, string> d = new Dictionary<string, string>();
      Console.ReadLine();
   }
   
   public void TestThreadPool()
   {
      System.Threading.WaitCallback waitCallback = new WaitCallback(MyThreadWork);
      ThreadPool.QueueUserWorkItem(waitCallback, "第1個綫程");
      ThreadPool.QueueUserWorkItem(waitCallback, "第2個綫程");
      ThreadPool.QueueUserWorkItem(waitCallback, "第3個綫程");
      ThreadPool.QueueUserWorkItem(waitCallback, "第4個綫程");
      Console.ReadLine();
   }
   
   private void MyThreadWork(object state)
   {
      Console.WriteLine("綫程正在啟動....{0}", (string)state);
      Thread.Sleep(10000);
      Console.WriteLine("綫程結束…… {0} (异步調用)", (string)state);
   }
   
   public void TestCustomAttribute()
   {
      Type ORM = typeof(CustomerModel);
      Console.WriteLine(ORM.FullName);
      
      object[] attrs = ORM.GetCustomAttributes(false);
      foreach (object o in attrs)
      {
         if (o is TableAttribute)
         Console.WriteLine("自定义特性:TableAttribute");
         else
         Console.WriteLine(o.GetType().FullName);
      }
      
      FieldInfo[] fs = ORM.GetFields();
      
      foreach (FieldInfo f in fs)
      {
         Console.WriteLine(f.GetType().FullName);
         
         
         object[] att = f.GetCustomAttributes(typeof(FieldAttribute), false);
         if (att.Length > 0)
         {
            if (att[0] is FieldAttribute)
            Console.WriteLine("自定义特性:FieldAttribute");
            else
            Console.WriteLine(att[0].GetType().FullName);
         }
      }
      Console.ReadLine();
   }
   
   public void TestSqlTranscation()
   {
      SqlConnection conn = this.CreateConnection();
      SqlTransaction tran = conn.BeginTransaction();
      try
      {
         //
         //.......處理資料...
         //....代碼省略....
         //
         tran.Commit();//提交事務
      }
      catch (Exception ex)
      {
         tran.Rollback();
      }
   }
   
   private SqlConnection CreateConnection()
   {
      return new SqlConnection();
   }
}

// 来源:www.CSFramework.com, C/S结构框架学习网




C/S框架网|csframework.com|C#源码




C/S框架网|原创精神.创造价值.打造精品


扫一扫加作者微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务

点击下载附件 点击下载附件 (如下载失败,请邮件通知我们寄回给您,或QQ:23404761留言.)
上一篇 下一篇