曾英-C#教学-44 命名空间和装箱拆箱数组
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace是本身定义的 namespace _44_命名空间
程序实例:函数
调用不一样命名空间中的类的方法: using System; using System.Collections.Generic; using System.Linq; using System.Text; //类库 namespace _44_命名空间 { class Program { static void Main(string[] args) { //引用别的命名空间的类的方法: //在类名前添加一个命名空间的名字 A.Bird f1 = new A.Bird(); f1.Fly(); //输出结果:A小鸟飞. //引用B命名空间的方法: B.Bird f2 = new B.Bird(); f2.Fly();//输出结果:B小鸟飞. } } } //类库 namespace A { class Bird { public void Fly() { Console.WriteLine("A小鸟飞"); } } } namespace B { class Bird { public void Fly() { Console.WriteLine("B小鸟飞"); } } }
程序实例:性能
//using System; //这里using System被注释了 /*using System.Collections.Generic; using System.Linq; using System.Text; */ namespace _44_2 { class Program { static void Main(string[] args) { //主函数中每个方法的前面都须要增长一个System.xxx来实现调用系统方法. double a = 4; double b = System.Math.Sqrt(a); System.Console.WriteLine(b); string cc = System.Console.ReadLine(); int d = System.Convert.ToInt32(b); } } }
using System; namespace _44_3_命名空间 { class Program { static void Main(string[] args) { //定义的时候要将两个命名空间依次写出 A.AA.bird f1=new A.AA.bird(); f1.Fly(); } } } //这里套用了两层命名空间. namespace A { namespace AA { class bird { public void Fly() { Console.WriteLine("a小鸟飞"); } } } namespace AB { } }
msdn学习
C#的类型分为值类型和引用类型,spa
值类型:装箱前n的值存储在栈中,blog
引用类型:装箱后n的值被封装到堆中.string
这里,什么类型都能赋给object型.可是会损失性能.it
程序实例:io
using System; using System.Collections.Generic; using System.Linq; using System.Text; //这里若是添加一个 using Ct,主函数中定义的时候就不须要写 Ct.Storehouse store = new Ct.Storehouse(5);中的Ct. namespace _44_装拆箱 { class Program { static void Main(string[] args) { Ct.Storehouse store = new Ct.Storehouse(5); //能够使用各类类型 store.Add(100); store.Add(2.14); store.Add("good"); store.Add(new Ct.Storehouse(5)); //遍历 //这里是装箱操做,将多种数据类型都用object类型. foreach (object item in store.Items) { Console.WriteLine(item); } } } } namespace Ct { class Storehouse { //object型数组,能够存储各类类型数据 public Object[] Items; private int count; //这里用count为数组计数 public Storehouse(int size) { //数组的初始化 Items = new Object[size]; count = 0; } //数组中添加数据 //为Items添加数据,并判断数组是否溢出 public void Add(Object obj) { if (count < Items.Length) { Items[count] = obj; count++; } else Console.WriteLine("仓库已满"); } } }