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结构框架学习网 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#.Net培训大纲(学习重点) FastReport for .Net 报表开发实例(C#源码下载) C#.Net WCF实例详解及源码下载 C#进程管理器源码(Managing .Net Process C#) 初学者:VS设计环境下建立SQL连接的数据源(图) C#.Net组件开发(高级篇) - 全部源码下载 关于C/S框架网C#.NET快速开发框架现场培训 .Net项目源码(C#+VS)成功案例展示中心 | C/S框架网 Winform自动升级框架源码(C#.NET)-C/S框架网 C#.NET iTextSharp生成PDF文件源码大全(生成表格、透明水印,页脚页眉页码等) C#源码-淘特旅游网站管理系统Asp.Net版 4.3-源码下载 C#源码-[CMS程序]LeadWit CMS.NET 1.0.1_leadwitcms-源码下载 C#源码-mrp生产管理系统(C#.NET)-源码下载 C#源码-OA网络办公自动化系统asp.NET-源码下载 C#源码-大型综合管理ERP源码 大型ERP源码 bs框架 asp.net源码 界面美观-源码下载
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |