to viena(维也纳nn) : C#支持指针操作,只不过我一直坚持纯面相对象,所以一直没有使用这种代码而已,而且我也承认我对C#的指针不熟悉。但是,骨子里那种从C程序员继承下来的高效,使我想知道C#中这个该怎么弄。
to fd7893(看着办吧):谢谢你的答案,但是我觉得帮助不大。我犯不着为了那么简单的事情写那么多代码,而且,你的代码效率远远不及Buffer.BlockCopy。我个人认为,不是越烦琐就越好,能一句话写完的,我决不写两句,这是我当年对C的感悟。
class myBytes
{
byte[] myValue;
public myBytes()
{
myValue = null;
}
public myBytes(int l)
{
myValue = new byte[l];
}
public int Length
{
get { return myValue.Length; }
}
public byte this[int i]
{
get { return myValue[i]; }
set { myValue[i] = value; }
}
public sbyte GetSbyte(int i)
{
return Convert.ToSByte(myValue[i]);
}
public void SetSbyte(int i, sbyte value)
{
myValue[i] = Convert.ToByte(value);
}
}
class Program
{
static void Main(string[] args)
{
myBytes b = new myBytes(25);
for (int i = 0; i < b.Length; i++)
b[i] = (byte)i;
for (int i = 0; i < b.Length;i++ )
Console.Write("{0,5}", b.GetSbyte(i));
Console.WriteLine();
Console.ReadLine();
}