C# 类如何声明索引器以提供对类的类似数组的访问的代码

时间:2022-12-16 09:06:07

研发期间,将内容过程中比较常用的内容段做个收藏,如下内容内容是关于 C# 类如何声明索引器以提供对类的类似数组的访问。的内容,希望能对各位有用处。

using System;
using System.IO;

public class FileByteArray
{
public FileByteArray(string fileName)
{
stream = new FileStream(fileName, FileMode.Open);
}

public void Close()
{
stream.Close();
stream = null;
}

{
get
{
byte[] buffer = new byte[1];
stream.Seek(index, SeekOrigin.Begin);
stream.Read(buffer, 0, 1);
return buffer[0];
}
set
{
byte[] buffer = new byte[1] {value};
stream.Seek(index, SeekOrigin.Begin);
stream.Write(buffer, 0, 1);
}
}

public long Length
{
get
{
return stream.Seek(0, SeekOrigin.End);
}
}
}

public class Reverse
{
public static void Main(String[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Usage : Indexer <filename>");
return;
}

if (!System.IO.File.Exists(args[0]))
{
Console.WriteLine("File " + args[0] + " not found.");
return;
}

FileByteArray file = new FileByteArray(args[0]);
long len = file.Length;

for (long i = 0; i < len / 2; ++i)
{
byte t;

t = file[i];
file[i] = file[len - i - 1];
file[len - i - 1] = t;
}

file.Close();
}
}