C#程序设计编程实例大全_第1页
C#程序设计编程实例大全_第2页
C#程序设计编程实例大全_第3页
C#程序设计编程实例大全_第4页
C#程序设计编程实例大全_第5页
已阅读5页,还剩44页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C C 程序设计编程实例大全程序设计编程实例大全 using System class hello static void Main Console WriteLine Hello World using System class Test static void Main int arr new int 1 2 3 int arr 1 2 3 for int i 0 i arr Length i Console WriteLine arr i using System class Test static void Main for int i 0 i 256 i if i 10 0 Console WriteLine Console WriteLine 0 3 1 3 i char i using System abstract class A protected int x public abstract void F public abstract int X get set class B A public override void F public override int X get return x set x value class Test static void Main B b new B b X 10 Console WriteLine b X using System struct MyClass public int i class Test static void Seti MyClass m m i 300 static void Main MyClass mc new MyClass mc i 100 MyClass mc1 mc Seti mc1 Console WriteLine mc i mc i Console WriteLine mc1 i mc1 i using System class method static void Main int i 5 int j 3 Console WriteLine i j add i j public static int add int i int j return i j using System using System Windows Forms class Fruit class Apple Fruit public int i 1 class Conversions static void Main Fruit f new Apple Type t f GetType Apple a Apple f Console WriteLine a i Console WriteLine Apple f i Fruit f new Fruit Apple a new Apple Console WriteLine f is Fruit Console WriteLine f is Apple Console WriteLine a is Fruit Console WriteLine a is Apple Fruit f new Apple if f is Apple Apple a Apple f Console WriteLine a i Fruit f new Apple Apple a f as Apple if a null Console WriteLine a i using System using System Collections class ArrList static void Main ArrayList arr new ArrayList string str1 while true Console WriteLine Please add a string to the ArrayList str1 Console ReadLine if str1 end break arr Add str1 Console WriteLine for int i 0 i arr Count i Console Write 0 arr i Console WriteLine n using System class A public A Console WriteLine 我是 类的无参构造函数 public A int i Console WriteLine 我是类 A 的带有一个参数的构造函数 class B A public B Console WriteLine 我是 B 类的无参构造函数 public B int i base i Console WriteLine 我是类 B 的带有一个参数的构造函数 class Test static void Main B b new B 100 using System class A public int i public string s class Test static void Main A a new A Console WriteLine i a i Console WriteLine s a s using System using System Windows Forms class Conversions static void Main Console WriteLine Byte MaxValue byte b 100 b byte b 100 b 100 Console WriteLine b using System class Test static void Main for int i 1 i 9 i for int j 1 j i j Console WriteLine 0 1 2 j i j i Console WriteLine n using System class A public void Main char a Console WriteLine a MaxValue using System class Test public static void Main int i j for i 1 i 9 i for j 1 j 9 i Console WriteLine i j add i j t public static int add int i int j return i j using System delegate void EatDelegate string food class Man private string name public Man string name this name name public void eat string food Console WriteLine name eat food class Party static void eatTogether string food params EatDelegate values if values null Console WriteLine the party is over else EatDelegate eatChain null foreach EatDelegate ed in values eatChain ed eatChain food Console WriteLine static void Main Man ZS new Man zhangsan Man LS new Man lisicuan Man WW new Man wangmazi EatDelegate zs new EatDelegate ZS eat EatDelegate ls new EatDelegate LS eat EatDelegate ww new EatDelegate WW eat EatDelegate eatChain null Console WriteLine the three person have a party eatTogether watermelon zs ls ww Console WriteLine lisicuan go to call eatTogether banana zs ww Console WriteLine lisicuan go back eatTogether orange zs ls ww eatTogether null null using System delegate void EatDelegate string food class MyDelegate static void zhangsanEat string food Console WriteLine zhangsan eat food static void lisicuanEat string food Console WriteLine lisicuan eat food static void wangmaziEat string food Console WriteLine wangmazi eat food static void Main zhangsanEat watermelon lisicuanEat watermelon wangmaziEat watermelon EatDelegate zhangsan new EatDelegate zhangsanEat EatDelegate lisicuan new EatDelegate lisicuanEat EatDelegate wangmazi new EatDelegate wangmaziEat zhangsan watermelon lisicuan watermelon wangmazi watermelon EatDelegate eatChain eatChain zhangsan lisicuan wangmazi eatChain watermelon Console WriteLine lisicuan go to call eatChain lisicuan eatChain banana Console WriteLine lisicuan go back eatChain lisicuan eatChain orange using System class A public static int i 100 static A Console WriteLine 我是类 的静态构造器 public A Console WriteLine 我是类 的无参构造函数 class Test static void Main Console WriteLine A i A a new A A b new A using System using System Collections interface IName string Name get set struct Person IName private string name public Person string name name name public string Name get return name set name value class Test static void Main ArrayList arr new ArrayList Person p new Person 张三 arr Add p IName arr 0 Name 李四 p Name 李四 Console WriteLine p Name Console WriteLine Person arr 0 Name using System class Method class A public static int i 5 public void addi i i 1 static void Main A a new A a addi A b new A b addi Console WriteLine A i using System class Method public static void ValueMethod int i i public static void ReferenceMethod ref int i i public static void OutputMethod out int i i 0 i static void Main int i 0 ValueMethod i Console WriteLine i i int j 0 ReferenceMethod ref j Console WriteLine j j int k OutputMethod out k Console WriteLine k k using System interface IA void F class B IA void IA F Console WriteLine B F class C B IA public void F Console WriteLine C F class Test static void Main C c new C IA c F using System class Student IComparable private string name private int age public Student string name int age name name age age public string Name get return name set name value public int Age get return age set age value public int CompareTo object right if right is Student throw new ArgumentException not the Student type return name CompareTo Student right name public override string ToString return name class Test static void Main Student arr new Student 5 arr 0 new Student 张三 4 arr 1 new Student 李四 5 arr 2 new Student 王五 3 arr 3 new Student 马六 1 arr 4 new Student 钱七 2 Array Sort arr foreach Student i in arr Console WriteLine i Name i Age using System using System Collections class Student IComparable private string name private int age public Student string name int age name name age age public string Name get return name set name value public int Age get return age set age value int IComparable CompareTo object right if right is Student throw new ArgumentException not the Student type return name CompareTo Student right name public int CompareTo Student right return name CompareTo Student right name private static AgeComparer ageCom null public static IComparer AgeCom get if ageCom null ageCom new AgeComparer return ageCom private class AgeComparer IComparer int IComparer Compare object left object right if left is Student right is Student throw new ArgumentException 参数必须为 Student return Student left age CompareTo Student right age public override string ToString return name class Test static void Main Student arr new Student 5 arr 0 new Student 张三 4 arr 1 new Student 李四 5 arr 2 new Student 王五 3 arr 3 new Student 马六 1 arr 4 new Student 钱七 2 Console WriteLine 按姓名排序 Array Sort arr foreach Student i in arr Console WriteLine i Name i Age Console WriteLine 按年龄排序 Array Sort arr Student AgeCom foreach Student i in arr Console WriteLine i Name i Age using System interface IDrivingLicence void GetLicence class Teacher IDrivingLicence public void Getlicence Console WriteLine 获得驾驶执照 class Test static void Main Teacher t new Teacher t Getlicence using System interface IDrivingLicenceB void GetLicence interface IDrivingLicenceA IDrivingLicenceB new void GetLicence class Teacher IDrivingLicenceA public void GetLicence Console WriteLine 老师获得 A 类驾驶执照 class Student IDrivingLicenceB public void GetLicence Console WriteLine 学生获得了 类驾驶执照 class Test static void DriveCar string name IDrivingLicenceB o IDrivingLicenceB dl o as IDrivingLicenceB if dl null Console WriteLine name 开动了卡车 else Console WriteLine name 没有驾驶执照 不能开车 static void DriveBus string name IDrivingLicenceB o IDrivingLicenceA dl o as IDrivingLicenceA if dl null Console WriteLine name 开动了公共汽车 else Console WriteLine name 没有驾驶执照 不能开车 static void Main Teacher t new Teacher DriveCar 教师 t DriveBus 教师 t Student s new Student DriveCar 学生 s DriveBus 学生 s using System interface IDrivingLicenceB void GetLicence interface IDrivingLicenceA IDrivingLicenceB new void GetLicence class Teacher IDrivingLicenceA void IDrivingLicenceB GetLicence Console WriteLine 老师获得 B 类驾驶执照 void IDrivingLicenceA GetLicence Console WriteLine 老师获得 A 类驾驶执照 public void GetLicence Console WriteLine 这个不是接口的方法 class Student IDrivingLicenceB public void GetLicence Console WriteLine 学生获得了 类驾驶执照 class Test static void DriveCar string name IDrivingLicenceB o IDrivingLicenceB dl o as IDrivingLicenceB if dl null Console WriteLine name 开动了卡车 else Console WriteLine name 没有驾驶执照 不能开车 static void DriveBus string name IDrivingLicenceB o IDrivingLicenceA dl o as IDrivingLicenceA if dl null Console WriteLine name 开动了公共汽车 else Console WriteLine name 没有驾驶执照 不能开车 static void Main Teacher t new Teacher DriveCar 教师 t DriveBus 教师 t Student s new Student DriveCar 学生 s DriveBus 学生 s IDrivingLicenceB t GetLicence IDrivingLicenceA t GetLicence t GetLicence using System interface IA void F interface IB IA new void F interface IC IA void G interface IBC IB IC class Derive IBC public void F Console WriteLine IB F public void G Console WriteLine IC G class Test static void Main Derive d new Derive d F IA d F IB d F IC d F IBC d F using System class Employee protected string name public Employee public Employee string name name name public virtual void StartWork Console WriteLine name 开始工作 class Manager Employee public Manager string name base name public override void StartWork base StartWork Console WriteLine 给员工下达任务 class Secrectary Employee public Secrectary string name base name public override void StartWork base StartWork Console WriteLine 协助经理 class Seller Employee public Seller string name base name public override void StartWork base StartWork Console WriteLine 销售产品 class Accountant Employee public Accountant string name base name public override void StartWork base StartWork Console WriteLine 管理公司财政 class Test static void Main Employee emp new Employee 5 emp 0 new Manager 张三 emp 1 new Secrectary 李四 emp 2 new Seller 王五 emp 3 new Seller 马六 emp 4 new Accountant 麻七 Console WriteLine 早上 点 开始工作 foreach Employee e in emp e StartWork using System public class MyResource IDisposable private bool disposed false public class Dispose Dispose true public void Close Dispose true MyResource Dispose false private void Dispose bool disposing if this disposed if disposing Console WriteLine 调用所引用的 Dispose 方法 Console WriteLine 释放类本身所使用的非托管资源 disposed true if disposing GC SuppressFinalize this public class Test static void Main MyResource mr new MyResource using System publc class MyResource IDisposable private bool disposed false public class Dispose Dispose true public void Close Dispose true MyResource Dispose false private void Dispose bool disposing if this disposed if disposing Console WriteLine 调用所引用的 Dispose 方法 using System class A public int i public string s public A public A int i this i i public A string s this s s public A int i string s this i i this s s class Test static void Main A a new A Console WriteLine The first constructor Console WriteLine a i Console WriteLine a s A b new A 1 Console WriteLine The second constructor Console WriteLine b i Console WriteLine b s A c new A 我是第三个构造器产生的 Console WriteLine The third constructor Console WriteLine c i Console WriteLine c s A d new A 3 我是第三个构造器产生的 Console WriteLine The forth constructor Console WriteLine d i Console WriteLine d s using System namespace CG public class PrintName public void intro Console WriteLine My name is A using System class Martrix static void Main int arr new int 4 6 for int i 0 i 4 i for int j 0 j 6 j arr i j i 1 10 j 1 for int i 0 i 4 i for int j 0 j 6 j Console Write 0 arr i j Console WriteLine using System class Method static void Main MyMethod public static void MyMethod Console WriteLine this is a method using System namespace CG class Test static void Main PrintName a new PrintName a intro using System class A public A Console WriteLine 类 A 被创建 A Console WriteLine 类 A 被释放 class Test static void Main for string s s end s Console ReadLine new A for int i 0 i 50 i byte b new byte 1000 using System class Test static void Main Console WriteLine u4F60 u597D using System using System Collections class IndexClass private Hashtable name new Hashtable public string this string index get return name index ToString set name Add index value class Test static void Main IndexClass b new IndexClass b A001 zhangsan b A002 lisicuan b A003 wangmazi Console WriteLine b A001 b A001 Console WriteLine b A002 b A002 Console WriteLine b A003 b A003 using System using System Collections class IndexClass private Hashtable name new Hashtable public string this int index get return name index ToString set name Add index value public int this string aName get foreach DictionaryEntry d in name if d Value ToString aName return Convert ToInt32 d Key return 1 set name Add value aName class Test static void Main IndexClass b new IndexClass b 100 zhangsan b 200 lisicuan b 300 wangmazi Console WriteLine the 100th person is b 100 Console WriteLine the 200th person is b 200 Console WriteLine the 300th person is b 300 using System using System Collections class IndexClass private Hashtable name new Hashtable public string this int index get return name index ToString set name Add index value class Test static void Main IndexClass b new IndexClass b 100 zhangsan b 200 lisicuan b 300 wangmazi Console WriteLine the 100th person is b 100 Console WriteLine the 200th person is b 200 Console WriteLine the 300th person is b 300 using System using System Collections class IndexClass private Hashtable name new Hashtable public string this int index get return name index ToString set name Add index value public int this string aName get foreach DictionaryEntry d in name if d Value ToString aName return Convert ToInt32 d Key return 1 set name Add value aName class Test static void Main IndexClass b new IndexClass b 100 zhangsan b 200 lisicuan b 300 wangmazi Console WriteLine the 100th person is b 100 Console WriteLine the 200th person is b 200 Console WriteLine the 300th person is b 300 Console WriteLine the 100th person s number is b zhangsan Console WriteLine the 200th person s number is b lisicuan Console WriteLine the 300th person s number is b wangmazi b maliu 400 b qianqi 500 Console WriteLine the 400th person is b 400 Console WriteLine the 500th person is b 500 using System class Testw static void Main char c a Console WriteLine int c using System class Publisher public delegate void PubComputer string magazineName public delegate void PubLife string magazineName public event PubComputer OnPubComputer public event PubLife OnPubLife public void issueComputer if OnPubComputer null Console WriteLine 发行 computer 刊物 OnPubComputer 电脑杂志 public void issueLife if OnPubLife null Console WriteLine 发行 life 刊物 OnPubLife 生活杂志 class Subscriber private string name public Subscriber string name this name name public void Receive string magazineName Console WriteLine name 已经收到 magazineName class Story static void Main Publisher Pub new Publisher Subscriber zs new Subscriber 张三 Pub OnPubComputer new Publisher PubComputer zs Receive Pub issueComputer using System class User private string name private string sex public void SetName string values name values public string GetName return name public void SetSex string values if values male values female sex values else Console WriteLine the sex is only male or female public string GetSex return sex public string Name get return name set name value public string Sex get return sex set if value male value female sex value else Console WriteLine the sex is only male or female class Property static void Main User zs new User zs SetName zhangsan zs SetSex female Console WriteLine NAME zs GetName SEX zs GetSex zs Name lisi zs Sex male Console WriteLine NAME zs Name SEX zs Sex using System class PubEventArgs EventArgs private readonly string m magazineName private readonly DateTime m pubDate public PubEventArgs string magazineName DateTime pubDate m magazineName magazineName m pubDate pubDate public string magazineName get return m magazineName public DateTime pubDate get return m pubDate class Publisher public delegate void PubComputerEventHandler object sender PubEventArgs e public delegate void PubLifeEventHandler object sender PubEventArgs e public event PubComputerEventHandler PubComputer public event PubLifeEventHandler PubLife protected virtual void OnPubComputer PubEventArgs e PubComputerEventHandler handler PubComputer if handler null handler this e protected virtual void OnPubLife PubEventArgs e PubLifeEventHandler handler PubLife if handler null handler this e public void issueComputer string magazineName DateTime pubDate Console WriteLine 发行 magazineName OnPubComputer new PubEventArgs magazineName pubDate public void issueLife string magazineName DateTime pubDate Console WriteLine 发行 magazineName OnPubLife new PubEventArgs magazineName pubDate class Subscriber private string name public Subscriber string name this name name public void Receive object sender PubEventArgs e Console WriteLine e pubDate name 已经收到 e magazineName class Story static void Main Publisher Pub new Publisher Subscriber zs new Subscriber 张三 Pub PubComputer new Publisher PubComputerEventHandler zs Receive Subscriber ls new Subscriber 李四 Pub PubComputer new Publisher PubComputerEventHandler ls Receive Pub PubLife new Publisher PubLifeEventHandler zs Receive Pub issueComputer 电脑杂志 Convert ToDateTime 2006 1 1 Pub issueLife 生活杂志 Convert ToDateTime 2006 1 1 Console WriteLine Con

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论