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结构框架学习网
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结构框架学习网
{
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结构框架学习网


参考文档:
.NET 开源免费图表组件库 - ScottPlot(Winform,WPF 通用)-C/S开发框架
FastReport.NET自动文本宽度+自动换行+自动行高|C/S开发框架
FastReport.NET2023破解版去除水印DEMO VERSION (2025.1.14/2023.2.18版本)|C/S开发框架
C/S软件授权注册系统V3.0(Winform+WebApi+.NET8+EFCore版本)|C/S开发框架
.NET C/S架构软件用户授权设计方案(mac地址+白名单用户)|C/S开发框架
企业级C/S系统快速开发框架(Winform+.NET8+EFCore)|C/S开发框架
.NETCore WebApi运行报错:System.AggregateException:“Some services are not able to be constructed|C/S开发框架
C#.NET各版本常用语法糖大全(C#3~C#10)|C/S开发框架
CSFramework.EF数据库框架应用Schema测试多种数据库(.NET8+EFCore)|C/S开发框架
CSFrameworV6试用版开发指南 | 主程序初始化CSFramework.EF数据库框架(.NET8)|C/S开发框架
.NET Reactor(.NET混淆器)加壳软件使用|C/S开发框架
CSFramework开发框架使用.NET Reactor加壳软件导致多语言失效|C/S开发框架
.NETCore WebApi后端开发框架+达梦数据库测试报告|C/S开发框架
C#.NET调用达梦数据库的存储过程返回多张表|C/S开发框架
C#.NET 达梦数据库存储过程 获取OUT参数返回值|C/S开发框架
SqlServer数据类型、DbType类型、.NET类型与达梦数据类型关系表|C/S开发框架
.NET 开源免费图表组件库 - ScottPlot(Winform,WPF 通用)-C/S开发框架
FastReport.NET自动文本宽度+自动换行+自动行高|C/S开发框架
FastReport.NET2023破解版去除水印DEMO VERSION (2025.1.14/2023.2.18版本)|C/S开发框架
C/S软件授权注册系统V3.0(Winform+WebApi+.NET8+EFCore版本)|C/S开发框架
.NET C/S架构软件用户授权设计方案(mac地址+白名单用户)|C/S开发框架
企业级C/S系统快速开发框架(Winform+.NET8+EFCore)|C/S开发框架
.NETCore WebApi运行报错:System.AggregateException:“Some services are not able to be constructed|C/S开发框架
C#.NET各版本常用语法糖大全(C#3~C#10)|C/S开发框架
CSFramework.EF数据库框架应用Schema测试多种数据库(.NET8+EFCore)|C/S开发框架
CSFrameworV6试用版开发指南 | 主程序初始化CSFramework.EF数据库框架(.NET8)|C/S开发框架
.NET Reactor(.NET混淆器)加壳软件使用|C/S开发框架
CSFramework开发框架使用.NET Reactor加壳软件导致多语言失效|C/S开发框架
.NETCore WebApi后端开发框架+达梦数据库测试报告|C/S开发框架
C#.NET调用达梦数据库的存储过程返回多张表|C/S开发框架
C#.NET 达梦数据库存储过程 获取OUT参数返回值|C/S开发框架
SqlServer数据类型、DbType类型、.NET类型与达梦数据类型关系表|C/S开发框架
其它资料:
什么是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内容管理系统 |
什么是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内容管理系统 |
在线客服
