蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 csc.exe (.NET Framework 4.x) 一维索引器、越界保护、字符串重载与二维多参索引器实测

CSHARPWindows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。ExploreIndexer.cs
ExploreIndexer.cs174 行
using System;
using System.Collections.Generic;
using System.Text;

namespace CSharpIndexerLab
{
    internal class IndexedNames
    {
        public static readonly int Size = 10;
        private readonly string[] namelist = new string[Size];

        public IndexedNames()
        {
            for (int i = 0; i < Size; i++)
            {
                namelist[i] = "N. A.";
            }
        }

        public string this[int index]
        {
            get
            {
                if (index >= 0 && index <= Size - 1)
                {
                    return namelist[index];
                }
                return "";
            }
            set
            {
                if (index >= 0 && index <= Size - 1)
                {
                    namelist[index] = value;
                }
            }
        }

        public int this[string name]
        {
            get
            {
                for (int i = 0; i < Size; i++)
                {
                    if (namelist[i] == name)
                    {
                        return i;
                    }
                }
                return -1;
            }
        }
    }

    internal class Grid2D
    {
        private readonly int[,] cells;

        public Grid2D(int rows, int cols)
        {
            cells = new int[rows, cols];
        }

        public int this[int row, int col]
        {
            get { return cells[row, col]; }
            set { cells[row, col] = value; }
        }
    }

    internal class Program
    {
        private static string Esc(string s)
        {
            if (s == null) return "null";
            StringBuilder sb = new StringBuilder("\"");
            foreach (char c in s)
            {
                switch (c)
                {
                    case '\\': sb.Append("\\\\"); break;
                    case '"': sb.Append("\\\""); break;
                    case '\n': sb.Append("\\n"); break;
                    case '\r': sb.Append("\\r"); break;
                    case '\t': sb.Append("\\t"); break;
                    default:
                        if (c < 32) sb.AppendFormat("\\u{0:x4}", (int)c);
                        else sb.Append(c);
                        break;
                }
            }
            sb.Append("\"");
            return sb.ToString();
        }

        private static string CaseJson(string name, string summary, string expected, string actual)
        {
            bool passed = string.Equals(expected, actual, StringComparison.Ordinal);
            return string.Format(
                "{{\"name\":{0},\"summary\":{1},\"expected\":{2},\"actual\":{3},\"passed\":{4}}}",
                Esc(name), Esc(summary), Esc(expected), Esc(actual), passed ? "true" : "false"
            );
        }

        public static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            List<string> cases = new List<string>();

            IndexedNames names = new IndexedNames();
            names[0] = "Zara";
            names[1] = "Riz";
            names[2] = "Nuha";
            names[3] = "Asif";
            names[4] = "Davinder";
            names[5] = "Sunil";
            names[6] = "Rubic";

            // Case 1: Int indexer virtual array traversal
            List<string> items = new List<string>();
            for (int i = 0; i < IndexedNames.Size; i++)
            {
                items.Add(names[i]);
            }
            cases.Add(CaseJson(
                "int_indexer_virtual_array",
                "Verify 1D int indexer get/set and default initialization for unassigned slots",
                "Zara,Riz,Nuha,Asif,Davinder,Sunil,Rubic,N. A.,N. A.,N. A.",
                string.Join(",", items.ToArray())
            ));

            // Case 2: Out-of-range bounds safety
            names[-1] = "InvalidLow";
            names[99] = "InvalidHigh";
            string actual2 = string.Format("low='{0}';high='{1}';slot0='{2}'", names[-1], names[99], names[0]);
            cases.Add(CaseJson(
                "indexer_bounds_safety",
                "Verify out-of-range get returns empty string and out-of-range set is safely ignored",
                "low='';high='';slot0='Zara'",
                actual2
            ));

            // Case 3: Overloaded string indexer
            int nuhaIdx = names["Nuha"];
            int missingIdx = names["Unknown"];
            string actual3 = string.Format("Nuha={0};Unknown={1}", nuhaIdx, missingIdx);
            cases.Add(CaseJson(
                "overloaded_string_indexer",
                "Verify overloaded string indexer resolves 'Nuha' to index 2 and missing entry to -1",
                "Nuha=2;Unknown=-1",
                actual3
            ));

            // Case 4: Multi-parameter 2D indexer
            Grid2D grid = new Grid2D(2, 3);
            grid[0, 1] = 15;
            grid[1, 2] = 42;
            string actual4 = string.Format("0,1={0};1,2={1};0,0={2}", grid[0, 1], grid[1, 2], grid[0, 0]);
            cases.Add(CaseJson(
                "multi_param_matrix_indexer",
                "Verify multi-parameter indexer this[int row, int col] on Grid2D",
                "0,1=15;1,2=42;0,0=0",
                actual4
            ));

            Console.WriteLine(string.Format(
                "{{\"version\":\"csc4/{0}\",\"cases\":[{1}]}}",
                Environment.Version.ToString(),
                string.Join(",", cases.ToArray())
            ));
        }
    }
}
本例需在 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。

入口文件:projects/csharp-indexer-lab/ExploreIndexer.cs

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 ExploreIndexer.cs,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C#

课程关联工程包(登记关联42章) · 含当前章节或实例 · mbskycom202612a1321.zip

下载课程关联工程包:C#

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1336.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习