1.命名空间
namespace 组织区分作用,组织不同类(可以同名)进入不同命名空间
程序(1)示例:
using A;
namespace _44_命名空间
{
internal class Program
{
static void Main(string[] args)
{
A.Bird f1 = new A.Bird();
f1.Fly();
B.Bird f2 = new B.Bird();
f2.Fly();
}
}
}
//类库。
namespace A
{
class Bird
{
public void Fly()
{
Console.WriteLine("A小鸟飞。");
}
}
}
namespace B
{
class Bird
{
public void Fly()
{
Console.WriteLine("B小鸟飞。");
}
}
}
程序(2)示例:
using System;
namespace _44_命名空间2
{
internal class Program
{
static void Main(string[] args)
{
double a = 4;
double b =System.Math.Sqrt(a);//可以在前面加上命名空间
System.Console.WriteLine(b);
string cc = Console.ReadLine();
int d=Convert.ToInt32(b);
}
}
}
.NET中部分命名空间
在命名空间中继续定义命名空间;
程序(3)示例:
using A;
namespace _44_命名空间
{
internal class Program
{
static void Main(string[] args)
{
A.AA.Bird f1 = new A.AA.Bird();
f1.Fly();
B.Bird f2 = new B.Bird();
f2.Fly();
}
}
}
//类库。
namespace A
{
namespace AA
{
class Bird
{
public void Fly()
{
Console.WriteLine("A小鸟飞。");
}
}
}
namespace AB
{
}
}
2.帮助文档
在VS中选择:Help<View Help
3.装箱和拆箱
-
装箱:装箱前n的值存储在栈中,装箱后被封装在堆中;
程序(4)示例:
namespace _44_装箱和拆箱 { internal class Program { static void Main(string[] args) { int n = 16; object obj = n;//装箱过程:值类型赋给引用类型 Console.WriteLine(obj); } } }
-
拆箱
程序(5)示例:
namespace _44_装箱和拆箱 { internal class Program { static void Main(string[] args) { int n = 16; object obj = n;//装箱过程:值类型赋给引用类型 Console.WriteLine(obj); int i = (int)obj;//拆箱:obj型转换为值类型 Console.WriteLine(i); } } }
** 装箱拆箱会带来性能损失**
程序(6)示例:
namespace _44_装箱和拆箱2 { internal class Program { static void Main(string[] args) { Ct.Storehouse store = new Ct.Storehouse(5); store.Add(100); store.Add(3.14); store.Add("Good"); store.Add(new Ct.Storehouse(5)); foreach(object item in store.Items) { Console.WriteLine(item); } } } } namespace Ct { class Storehouse { public Object[] Items; private int count; public Storehouse(int size) { Items = new Object[size]; count = 0; } public void Add(Object obj) { if(count<Items.Length) { Items[count] = obj; count++; } else { Console.WriteLine("Storage if full!"); } } } }