C# 索引器
来源:互联网 发布:龙视安网络摄像头协议 编辑:程序博客网 时间:2024/05/16 11:41
以下是索引器例子程序,功能是通过索引器查看某个数字,某一位(二进制下)是否为0,0则False,1则true。
还可以通过true和false 设置某一位的值为1还是0;
struct IntBits
{public IntBits(int initialBitValue)
{
bits = initialBitValue;
}
private int bits;
public bool this[int index]
{
get
{
return (bits & (1 << index)) != 0;
}
set
{
if (value)
bits |= (1 << index);
else
bits &= ~(1 << index);
Console.Write("值为:{0}, 二进制为:", bits);
Console.WriteLine(Convert.ToString(bits, 2));
}
}
}
static void Main(string[] args)
{
int adapted = 62;
Console.WriteLine("初始值为:{0},二进制为:{1}",adapted,Convert.ToString(adapted, 2));
IntBits bits = new IntBits(adapted);
bool peek = bits[6];
Console.WriteLine("二进制第6位的值是否为1,1则True,0则False :{0}",peek);
bits[0] = true;
bits[3] = false;
}
索引器使用注意事项:
1、索引器不仅能用整型数值做下标,还可以用string等。
2、索引器可以像方法一样被重载。
3、索引器不能作为ref和out修饰的参数。
在上面结构体内,还可以加这个,通过参数的不同,形成重载。
public bool this[string index]
{
get
{
return true;
}
}
接口中也可以有索引器。类中实现索引器是也可以将索引器声明为虚的索引器(这个暂时没成功,提示不能用virtual声明this)。
interface IRawInt
{
bool this[int index] { get; set; }
}
- C#索引器
- C#索引器
- C#索引器
- C#中的索引器
- C#索引器
- C# 索引器
- C#索引器
- C#索引器
- C# 索引器
- C#索引器
- C#索引器
- C#索引器
- C#索引器
- c#属性 索引器
- C#的索引器
- C#索引器
- C#索引器
- C#索引器
- oracle将一个字段拆分成多个值(regexp_substr函数)
- linux alias命令使用笔记
- JavaScript二叉查找树删除节点的实现原理
- Eclipse快捷键-方便查找
- 13.剑指offer-二叉树的下一个节点
- C# 索引器
- JDBC事务
- Wrong committed revision number: -1
- Dos 以del为例的参数与作用(/f/s/q)
- 浅析session、cookie原理机制
- 用MAT解决GridView引起的内存泄漏
- 高性能mysql之前缀索引
- jsp页面跳转
- 手游客户端设计思路整理