base 关键字
见 msdn:http://msdn.microsoft.com/zh-cn/library/hfw7t1ce.aspx
this 关键字
见 msdn:http://msdn.microsoft.com/zh-cn/library/dk1507sz.aspx
一. 将class封装成dll
在vs tools中点击vs command prompt,输入如下命令:
csc /target:library 文件名.cs
注意: 文件路径要正确。
二. 将主文件和dll关联
csc /reference:dll文件名.dll 主文件名.cs
会生成 主文件名.exe 文件, 执行 exe文件,就可以看到运行结果。
三. 值传递和引用传递,输出参数的调用实例
namespace Method21 { class Program { static void ValueMethod(int i) { i++; } static void ReferenceMethod(ref int i) { i++; } static void OutMethod(out int i) { i = 0; i++; } static void Main(string[] args) { int i = 0; int j = 0; int k; ValueMethod(i); ReferenceMethod(ref j); OutMethod(out k); Console.WriteLine("i={0}",i); Console.WriteLine("j={0}",j); Console.WriteLine("k={0}",k); Console.ReadKey(); } } }
运行结果:
i=0
j=1
k=1
四. 可变参数函数
namespace Method21 { class Program { static int addi(params int[] values) { int sum = 0; foreach (int i in values) { sum = sum + i; } return sum; } static void Main(string[] args) { Console.WriteLine(addi(1, 2, 3, 4, 5, 6)); Console.ReadKey(); } } }
五. 类型转换
namespace convert21 { class Fruit { } class Apple:Fruit { public int i = 1; } class Program { static void Main(string[] args) { Fruit f = new Apple(); Console.WriteLine(((Apple)f).i); Console.ReadKey(); } } }
六. 委托
(1)委托,委托链(静态)
namespace delegate1 { class MyDelegate { delegate void EatDelegate(string sFood); static void zsEat(string sFood) { Console.WriteLine("张三吃 "+sFood); } static void lsEat(string sFood) { Console.WriteLine("李四吃 " + sFood); } static void wwEat(string sFood) { Console.WriteLine("王五吃 " + sFood); } static void Main(string[] args) { EatDelegate zs = new EatDelegate(zsEat); EatDelegate ls = new EatDelegate(lsEat); EatDelegate ww = new EatDelegate(wwEat); EatDelegate EatChain = zs + ls + ww; EatChain("西瓜"); Console.ReadKey(); } } }
(2). 匿名方法
namespace delegate11 { delegate void EatDelegate(string sFood); class MyDelegate { static void zsEat(string sFood) { Console.WriteLine("张三吃 "+sFood); } static void lsEat(string sFood) { Console.WriteLine("李四吃 " + sFood); } static void wwEat(string sFood) { Console.WriteLine("王五吃 " + sFood); } static void Main(string[] args) { EatDelegate Eatchain = null; Eatchain += delegate(string sFood) { Console.WriteLine("张三吃 " + sFood); }; Eatchain += delegate(string sFood) { Console.WriteLine("李四吃 " + sFood); }; Eatchain += delegate(string sFood) { Console.WriteLine("王五吃 " + sFood); }; Eatchain("西瓜"); Console.ReadKey(); } } }
(3)委托,委托链(动态 类)
namespace delegate2 { delegate void EatDelegate(string sFood); class Man { private string _name; public Man(string name) { _name = name; } public void eat(string food) { Console.WriteLine(_name + "吃" + food); } } class Party { static void eatTogether(string food, params EatDelegate[] values) { if (values == null) { Console.WriteLine("聊天结束了..."); } else { EatDelegate EatChain = null; foreach (EatDelegate ed in values) { EatChain += ed; } EatChain(food); } Console.WriteLine(); } static void Main(string[] args) { Man zs = new Man("张三"); Man ls = new Man("李四"); Man ww = new Man("王五"); EatDelegate ZS = new EatDelegate(zs.eat); EatDelegate LS = new EatDelegate(ls.eat); EatDelegate WW = new EatDelegate(ww.eat); Console.WriteLine("张三,李四,王五 在聊天......"); eatTogether("Watermelon", ZS, LS, WW); Console.WriteLine("李四 出去接电话......"); eatTogether("Banana", ZS,WW); Console.WriteLine("李四 回来了......"); eatTogether("Orange", ZS, LS, WW); eatTogether(null,null); Console.ReadKey(); } } }
(4) 实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace delegate01 { class DelegateSample { public delegate bool ComparisonHandler(int first, int second); public static void BubbleSort(int[] items, ComparisonHandler comparisonMethod) { int i, j, temp; for (i = items.Length - 1; i >= 0; i--) { for (j = 1; j <= i; j++) { if (comparisonMethod(items[j - 1], items[j])) { temp = items[j - 1]; items[j - 1] = items[j]; items[j] = temp; } } } } public static bool GreaterThan(int first, int second) { return first > second; } public static bool AlphabeticalGreaterThan(int first, int second) { int comparison; comparison = (first.ToString().CompareTo(second.ToString())); return comparison > 0; } static void Main(string[] args) { int i; int[] items = new int[5]; for (i = 0; i < items.Length; i++) { Console.Write("Enter an integer: "); items[i] = int.Parse(Console.ReadLine()); } BubbleSort(items, AlphabeticalGreaterThan); for (i = 0; i < items.Length; i++) { Console.WriteLine(items[i]); } Console.ReadKey(); } } }
7 索引器
http://www.cnblogs.com/jarod99/archive/2009/01/09/1372453.html
using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace basic01 { 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 Program { static void Main(string[] args) { IndexClass b = new IndexClass(); b[500] = "Zhang San"; b[300] = "Li Si"; b[400] = "Wang Wu"; Console.WriteLine("b[500] = " + b["Zhang San"]); Console.WriteLine("b[300] = " + b["Li Si"]); Console.WriteLine("b[400] = " + b["Wang Wu"]); b[600] = "Li Wu"; b[700] = "Wang Liu"; Console.WriteLine("b[600] = " + b[600]); Console.WriteLine("b[700] = " + b[700]); Console.ReadKey(); } } }