C# .Net 实例积累

来源:互联网 发布:旅行社 知乎 编辑:程序博客网 时间:2024/05/13 01:41

一.将class封装成dll

在vs tools中点击vs commandprompt,输入如下命令:

csc /target:library文件名.cs

注意: 文件路径要正确。

二. 将主文件和dll关联

csc /reference:dll文件名.dll  主文件名.cs

会生成主文件名.exe 文件, 执行 exe文件,就可以看到运行结果。

三.值传递和引用传递实例

using System;

 

namespace Method21

{

    classProgram

    {

       static void ValueMethod(inti)

      i++; }

       static voidReferenceMethod(ref int i)

      i++; }

       static void Main(string[]args)

       {

          int i = 0;

          int j = 0;

          ValueMethod(i);

          ReferenceMethod(ref j);

          Console.WriteLine("i={0}",i);

          Console.WriteLine("j={0}",j);

          Console.ReadKey();

       }}

运行结果:

i=0

j=1





 

1. 类、对象、接口、继承 实例:

 

 usingSystem;
using System.Collections.Generic;
using System.Text;

namespace _9ex01
{
    publicabstract class MyBase
    {
    }
    internalclass MyClass  : MyBase
    {
    }
    publicinterface IMyBaseInterface
    {
   
    }
    internalinterface IMyBaseInterface2
    {
   
    }
    internalinterface MyInterface   IMyBaseInterface,IMyBaseInterface2
    {
   
    }
    internalsealed classMyComplexClass    MyClass,   MyInterface
    {
   
    }

   class Program
 {
       static void Main(string[] args)
       {
           MyComplexClass myObj = new MyComplexClass();
           Console.WriteLine(myObj.ToString());
           Console.ReadKey();

       }
    }
}

 

2. 索引器,数组,函数重载

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace indexer
{
    classArrClass
    {
       private readonly string name;
       public ArrClass(String name)
       { this.name = name; }
       public string Name
       {
           get { return name; }
       }
    }
    classIndexClass
    {
       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); }
       }
    }
    classProgram
    {
       static void Main(string[] args)
       {
           ArrClass[] a = new ArrClass[10];
           a[0] = new ArrClass("张三");
           a[1] = new ArrClass("李四");
           a[2] = new ArrClass("王五");
           Console.WriteLine("a[0]=" + a[0].Name);
           Console.WriteLine("a[1]=" + a[1].Name);
           Console.WriteLine("a[2]=" + a[2].Name);
           Console.WriteLine();
           IndexClass b = new IndexClass();
           b[500] = "张三";
           b[300] = "李四";
           b[400] = "王五";
           Console.WriteLine("b[500] = " + b["张三"]);
           Console.WriteLine("b[300] = " + b["李四"]);
           Console.WriteLine("b[400] = " + b["王五"]);
           b[600] = "李五";
           b[700] = "王六";
           Console.WriteLine("b[600] = " + b[600]);
           Console.WriteLine("b[700] = " + b[700]);
           Console.ReadKey();
       }
    }
}

 3.委托,委托链

 

静态方法:

using System;
using System.Collections.Generic;
using System.Text;

namespace delegate1
{
   delegate voidEatDelegate(string food);

   classMydelegate
    {
       static void zsEat(string food)
       {
           Console.WriteLine("张三吃 " + food);
       }
      static void lsEat(string food)
      {
          Console.WriteLine("李四吃 " + food);
      }
      static void wwEat(string food)
      {
          Console.WriteLine("王五吃 " + food);
      }
       static void Main()
       {
           EatDelegate zs = new EatDelegate(zsEat);
           EatDelegate ls = new EatDelegate(lsEat);
           EatDelegate ww = new EatDelegate(wwEat);
           //zs("西瓜");
           EatDelegate eatChain;
           Console.WriteLine("张三,李四,王五 开party");
           eatChain = zs + ls + ww;
           eatChain("西瓜");
           Console.WriteLine();
           Console.WriteLine("张三 有事离开。");
           eatChain -= zs;
           eatChain("桃子");
           Console.WriteLine();
           Console.WriteLine("张三 回来了。");
           eatChain += zs;
           eatChain("西瓜");
           Console.ReadKey();
              
    }

}

动态方法:

using System;
using System.Collections.Generic;
using System.Text;

namespace delegate1
{
   delegate voidEatDelegate(string food);
   class Man
    {
    privatestring name;
    publicMan(string name)
    {
    this.name=name;
    }
    public voideat(string food)
    {
    Console.WriteLine(name+" 吃"+food);
    }
       
 class Program
 {
     static voidMain(string[] args)
     {
     Man ZS=new Man("张三");
     Man LS=new Man("李四");
     Man WW=newMan("王五");         
     EatDelegate zs = new EatDelegate(ZS.eat);
     EatDelegate ls = new EatDelegate(LS.eat);
     EatDelegate ww = newEatDelegate(WW.eat);

           EatDelegate eatChain;
           Console.WriteLine("张三,李四,王五 开party");
           eatChain = zs + ls + ww;
           eatChain("西瓜");
           Console.WriteLine();
           Console.WriteLine("张三 有事离开。");
           eatChain -= zs;
           eatChain("桃子");
           Console.WriteLine();
           Console.WriteLine("张三 回来了。");
           eatChain += zs;
           eatChain("西瓜");
           Console.ReadKey();
    }
}
}

 4.委托,事件

 

 usingSystem;
using System.Collections.Generic;
using System.Text;

namespace @event01
{
    classPublisher
    {
       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("发行<电脑>杂志");
               onPubComputer("电脑杂志");
           }
       }
       public void issueLife()
       {
           if (onPubLife != null)
           {
               Console.WriteLine("发行<生活>杂志");
               onPubLife("生活杂志");
           }
       }
    }
    classSubcriber
    {
       private string name;
       public Subcriber(string name)
       {
           this.name = name;
       }
       public void Receive(string magazineName)
       {
           Console.WriteLine(name+"订阅者已经收到了"+magazineName);
       }
    }

   class Program
    {
       static void Main()
       {
           Publisher Pub = new Publisher();
           Subcriber zs = new Subcriber("张三");
           Pub.onPubComputer += new Publisher.PubComputer(zs.Receive);
           Subcriber ls = new Subcriber("李四");
           Pub.onPubComputer += new Publisher.PubComputer(ls.Receive);
           Pub.onPubLife += new Publisher.PubLife(ls.Receive);

           Pub.issueComputer();
           Pub.issueLife();
           Console.WriteLine();
           Console.WriteLine("1 year later:");
           Pub.onPubComputer -= new Publisher.PubComputer(ls.Receive);
           Pub.issueComputer();
           Pub.issueLife();
           Console.ReadKey();
       }
    }
}

 5.虚方法,多态,继承

 

 usingSystem;
using System.Collections.Generic;
using System.Text;

namespace virtual2
{
    classEmployee
    {
       protected string _Name;
       public Employee() { }
       public Employee(string name)
       {
           _Name = name;
       }
       public virtual void StartWork()
       {
           Console.WriteLine(_Name+"开始工作: ");
       }
    }
    classManager : Employee
    {
       public Manager(string name) : base(name) { }
       public override void StartWork()
       {
           base.StartWork();
           Console.WriteLine("给员工下达任务");
       }
    }
    classSecretary : Employee
    {
       public Secretary(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("销售产品");
       }
    }
    classAccountant : Employee
    {
       public Accountant(string name) : base(name) { }
       public override void StartWork()
       {
           base.StartWork();
           Console.WriteLine("管理公司财政");
       }
    }
    classProgram
    {
       static void Main(string[] args)
       {
           Employee[] emp = new Employee[5];
           emp[0]=new Manager("张三 经理 ");
           emp[1]=new Secretary("李奇 秘书 ");
           emp[2]=new Seller("王五 销售人员 ");
           emp[3] = new Seller("钱六 销售人员 ");
           emp[4]=new Accountant("赵四 财务人员 ");
           foreach (Employee e in emp)
           {
               e.StartWork();
           }
           Console.ReadKey();
       }
    }
}

 6. 类型转换

 

例子一using System;
using System.Collections.Generic;
using System.Text;

namespace convert21
{
    classFruit
    {
    }
    class Apple: Fruit
 {
  public int i=1;
     
    classProgram
    {
       static void Main(string[] args)
       {
           Fruit f = new Apple();
           Apple a = f as Apple;
           if (a!=null)
           {
               Console.WriteLine(a.i);
           }
           Console.ReadKey();
       }
    }
}

 例子二(部分代码)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace convert22
{
    publicpartial class Form1 : Form
    {
       public Form1()
       {
           InitializeComponent();
       }

       private void button3_Click(object sender, EventArgs e)
       {
           foreach (Control c in this.Controls)
           {
               listBox1.Items.Add(c.Name);
               Control c1 = c as Button;
               if (c1 != null)
               {
                   c1.Text = "按钮";
               }
           }
       }
    }
}

  7. IComparable,IComparer,类型实现多种排序应用

 

 代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace IComparable01
{
    classStudent: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("Argument type must beStudent.");
           }
           return _name.CompareTo(((Student)right)._name);
       }
       public int CompareTo(Student right)
       {
           return _age.CompareTo(right._age);
       }
       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("Argument type must beStudent.");
               }
               return ((Student)left)._age.CompareTo(((Student)right)._age);
                   
       }
       public override string ToString()
       {
           return _name;
       }
    }
    classProgram
    {
       static void Main(string[] args)
       {
           Student[] arr = new Student[5];
           arr[0] = new Student("Cheers Li", 28);
           arr[1] = new Student("Huichang Lee", 30);
           arr[2] = new Student("Linxiang Zhao", 29);
           arr[3] = new Student("Ming zhang", 27);
           arr[4] = new Student("Guohai Gu", 31);
           Console.WriteLine("...Sorted by name...");
           Array.Sort(arr);
           foreach(Student i in arr)
           {
               Console.WriteLine(i+" "+i.Age);
           }
           Console.WriteLine("...Sorted by age...");
           Array.Sort(arr,Student.AgeCom);
           foreach (Student i in arr)
           {
               Console.WriteLine(i + " " + i.Age);
           }
           Console.ReadKey();          
       }
    }
}

 

 

原创粉丝点击