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


  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框架网|原创作品.质量保障.竭诚为您服务

版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
C/S框架网
上一篇:解决方案:(Error 13 未能找到文件 \Properties\licenses.licx)
下一篇:C#.Net OOP系列之接口设计及策略应用实战
评论列表

发表评论

评论内容
昵称:
关联文章

C#初学者:分享一些C#.Net培训
C#使用Graphics合成二维和头像的分享图(小程序分享、App分享)
C/S快速开发框架能提供销存参考吗?
C#.Net培训大纲(学习重点)
原创:CodeHighlighter源代码格式化,代码缩,关键词高亮着色(C#)
.Net项目(C#+VS)成功案例展示中心 | C/S框架网
C#进程管理器(Managing .Net Process C#)
FastReport for .Net 报表开发实例(C#下载)
软件开发与设计 - MRP-物料需求计划-仓底货-销存
C#.Net WCF实例详解及下载
学习C#.NET基础知识(学习重点下载附件)
Mini-Framework蝇量框架 - 产品报价(全部)
C#.Net组件开发(高级篇) - 全部下载
初学者:VS设计环境下建立SQL连接的数据(图)
Winform自动升级框架(C#.NET)-C/S框架网
DevExpress XtraReport报表实例下载(C#)
关于C/S框架网C#.NET快速开发框架现场培训
C#制作透明背景GIF动画(演示+
CSFrameworkV6旗舰版展示(C#,LINQ+EF)
OA管理模块 - 职员培训 - TMS - 物流运输管理系统

热门标签
.NET5 .NET6 .NET7 APP Auth-软件授权注册系统 Axios B/S B/S开发框架 Bug Bug记录 C#加密解密 C#源码 C/S CHATGPT CMS系统 CodeGenerator CSFramework.DB CSFramework.EF CSFrameworkV1学习版 CSFrameworkV2标准版 CSFrameworkV3高级版 CSFrameworkV4企业版 CSFrameworkV5旗舰版 CSFrameworkV6.0 DAL数据访问层 Database datalock DbFramework Demo教学 Demo下载 DevExpress教程 DOM EF框架 Element-UI EntityFramework ERP ES6 Excel FastReport GIT HR IDatabase IIS JavaScript LINQ MES MiniFramework MIS NavBarControl Node.JS NPM OMS ORM PaaS POS Promise API Redis SAP SEO SQL SQLConnector TMS系统 Token令牌 VS2022 VSCode VUE WCF WebApi WebApi NETCore WebApi框架 WEB开发框架 Windows服务 Winform 开发框架 Winform 开发平台 WinFramework Workflow工作流 Workflow流程引擎 版本区别 报表 踩坑日记 操作手册 代码生成器 迭代开发记录 基础资料窗体 架构设计 角色权限 开发sce 开发技巧 开发教程 开发框架 开发平台 开发指南 客户案例 快速搭站系统 快速开发平台 秘钥 密钥 权限设计 软件报价 软件测试报告 软件简介 软件开发框架 软件开发平台 软件开发文档 软件体系架构 软件下载 软著证书 三层架构 设计模式 生成代码 实用小技巧 收钱音箱 数据锁 数据同步 微信小程序 未解决问题 文档下载 喜鹊ERP 喜鹊软件 系统对接 详细设计说明书 行政区域数据库 需求分析 疑难杂症 蝇量级框架 蝇量框架 用户管理 用户开发手册 用户控件 在线支付 纸箱ERP 智能语音收款机 自定义窗体 自定义组件 自动升级程序