How a cc class declares an indexer to provide access to a similar array of classes.

From , 4 Years ago, written in C#, viewed 53 times.
URL https://pastebin.vip/view/0887f1a5
  1.  
  2. // indexer.cs
  3. // 参数:indexer.txt
  4. using System;
  5. using System.IO;
  6.  
  7. // 将大文件当作字节数组
  8. // 访问的类。
  9. public class FileByteArray
  10. {
  11.     Stream stream;      // 包含用于访问
  12.                         // 该文件的基础流。
  13. // 创建封装特定文件的新 FileByteArray。
  14.     public FileByteArray(string fileName)
  15.     {
  16.         stream = new FileStream(fileName, FileMode.Open);
  17.     }
  18.  
  19.     // 关闭流。这应是
  20.     // 结束前的最后一个操作。
  21.     public void Close()
  22.     {
  23.         stream.Close();
  24.         stream = null;
  25.     }
  26.  
  27.     // 提供对文件的读/写访问的索引器。
  28.     public byte this[long index]   // long 是 64 位整数
  29.     {
  30.         // 在偏移量 index 处读取一个字节,然后将其返回。
  31.         get
  32.         {
  33.             byte[] buffer = new byte[1];
  34.             stream.Seek(index, SeekOrigin.Begin);
  35.             stream.Read(buffer, 0, 1);
  36.             return buffer[0];
  37.         }
  38.         // 在偏移量 index 处写入一个字节,然后将其返回。
  39.         set
  40.         {
  41.             byte[] buffer = new byte[1] {value};
  42.             stream.Seek(index, SeekOrigin.Begin);
  43.             stream.Write(buffer, 0, 1);
  44.         }
  45.     }
  46.  
  47.     // 获取文件的总长度。
  48.     public long Length
  49.     {
  50.         get
  51.         {
  52.             return stream.Seek(0, SeekOrigin.End);
  53.         }
  54.     }
  55. }
  56.  
  57. // 演示 FileByteArray 类。
  58. // 反转文件中的字节。
  59. public class Reverse
  60. {
  61.     public static void Main(String[] args)
  62.     {
  63.                 // 检查参数。
  64.                 if (args.Length != 1)
  65.                 {
  66.                         Console.WriteLine("Usage : Indexer <filename>");
  67.                         return;
  68.                 }
  69.  
  70.                 // 检查文件是否存在
  71.                 if (!System.IO.File.Exists(args[0]))
  72.                 {
  73.                         Console.WriteLine("File " + args[0] + " not found.");
  74.                         return;
  75.                 }
  76.  
  77.                 FileByteArray file = new FileByteArray(args[0]);
  78.                 long len = file.Length;
  79.  
  80.                 // 交换文件中的字节以对其进行反转。
  81.                 for (long i = 0; i < len / 2; ++i)
  82.                 {
  83.                         byte t;
  84.  
  85.                         // 请注意,为“file”变量建立索引会调用
  86.                         //  FileByteStream 类上的索引器,该索引器在文件中读取
  87.                         // 和写入字节。
  88.                         t = file[i];
  89.                         file[i] = file[len - i - 1];
  90.                         file[len - i - 1] = t;
  91.                 }
  92.  
  93.                 file.Close();
  94.     }
  95. }
  96.  
  97. //csharp/4916

Reply to "How a cc class declares an indexer to provide access to a similar array of classes."

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website