1、引用命名空间的方法是利用using指令,使用格式如下:
using【别名=】命名空间 或 using【别名=】命名空间.成员
using mysn = System.Math; //声明命名空间中某个类的别名
namespace 命名空间
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(mysn.Sin(0.5)); //以别名为前缀
Console.ReadKey();
}
}
}
注:使用using System.Math是错误的,因为System.Math是一个类而不是命名空间
2、在程序中声明命名空间Ns1,并定义对象:
namespace Ns1 //声明
{
class A{}
}
Ns1.A a=new Ns1.A(); //定义
索引器提供了一种访问类或结构的方法,即允许按照和数组相同的方式对类、结构或接口进行索引
声明索引器,需要用到this关键字:
public int this[索引类型 index]
{
//get和set访问器
}
索引器使得对象可按照与数组相似的方法进行索引
get 访问器返回值。set 访问器分配值
this 关键字用于定义索引器
value 关键字用于定义由 set 索引器分配的值
namespace 所引器
{
class Program
{
static void Main(string[] args)
{
int[] value = { 2, 5, 8, 9 }; //定义一个数组
int i = value[1];
person p1 = new person();
p1[1] = "哈哈"; //p1[1]赋值,调用set{}
Console.WriteLine(p1[1]+p1[2]); //p1[2]取值,调用get{}
person p2 = new person();
p2[1] = "嘻嘻";
Console.WriteLine(p2[1]);
Console.ReadKey();
}
}
class person
{
private string name1 = "大猫";
private string name2 = "二毛";
public string this [int index]
{
set
{
if (index==1)
{
name1 = value;
}
else if (index == 2)
{
name2 = value;
}
else
{
throw new Exception("你是智障吗");
}
}
get
{
if (index == 1)
{
return name1;
}
else if (index == 2)
{
return name2;
}
else
{
throw new Exception("你是傻逼吗");
}
}
}
}
}
这两天都很难受,现在在听蔡健雅的《十万毫升泪水》(>人<;)