是否有等同于 MS DOS BASIC 指令的数据、恢复和读取?

Is there an equivalent to MS DOS BASIC instructions like data, restore and read?

我对 C# 有点陌生,我正在尝试编写代码,在其中嵌入 table 永远不会更改的数据。是否有等同于 MS DOS BASIC 指令的数据、恢复和读取?请看看我贴在下面的区块...

    1170 '
    1180 ' Error trapping routine.
    1190 '
    1200 EN=ERR:EL=ERL:' error number:error line.
    1210 IF EN=53 AND EL=460 THEN RESUME 480
    1220 IF EN=53 THEN EXIST=0:RESUME 880
    1230 SCREEN 0:PRINT
    1240 RESTORE 1470:' Error data
    1250 FOR MYERR=1 TO 35
    1260 READ ERNUM,ERTYPE$
    1270 IF ERNUM=EN THEN 1290
    1280 NEXT
    1290 PRINT "Error #"EN"("ERTYPE$") in line"EL
    1300 PRINT:LIST 510-610
    1310 '
    1440 '
    1450 ' Error listing.
    1460 '
    1470 DATA 2,A sintaxis error has been found
    1480 DATA 3,Path not found
    1490 DATA 4,There is no more data
    1500 DATA 5,An Illegal function call has been found
    1510 DATA 6,An overflow error has occurred
    1520 DATA 7,Not enought memory!
    1530 DATA 8,Undefined line number
    1540 DATA 9,Index out of range
    1550 DATA 11,Division by zero error
    .
    .
    .

尝试这样的事情:

           object[][] data = new object[][] {
                new object[] {2, "A sintaxis error has been found"},
                new object[] {3, "Path not found"},
                new object[] {4, "There is no more data"},
                new object[] {5, "An Illegal function call has been found"},
                new object[] {6, "An overflow error has occurred"},
                new object[] {7, "Not enought memory!"},
                new object[] {8, "Undefined line number"},
                new object[] {9, "Index out of range"},
                new object[] {11, "Division by zero error"}
            };

            int en = 1234;
            int el = 12;
            foreach(object[] d in data)
            {
                if ((int)d[0] == en)
                {
                    Console.WriteLine("Error #{0} in line {1}", (string)d[1], el); 
                }
            }

DATA 语句定义了“table”中的一行,然后您对其进行迭代:RESTORE 设置“迭代器”,READ 取消引用迭代器and 将其推进到下一项。这本质上是一个键值“字典”,您可以使用 C# 的 Dictionary 容器:

using System;
using System.IO;
using System.Collections.Generic;
                    
public class Program
{
    Dictionary<int, string> errors = new Dictionary<int, string>() {
        [2] = "A syntax error has been found",
        [3] = "Path not found",
        [4] = "There is no more data" // etc.
    };
    

如果您确实需要这样的字典,这就是您的处理方式。 C# 提供了强大的错误诊断功能,您无需手动管理错误消息字典。这些错误称为异常,可以捕获异常,它们传达的信息比大多数 BASIC 中的信息多得多。

这是处理文件打开失败的最接近的等效代码:

    void someMethod() {
        object stream;
        try {
            // Presumed equivalent of line 460
            stream = File.Open("path", FileMode.Open, FileAccess.Read);
            // Line 470 would be here
        } catch (FileNotFoundException) {} // if the file didn't open, we just continue
        // Line 480 onwards would be here
    }

下面是处理任意错误的方法,不仅提供它们的描述,还提供一个堆栈跟踪,显示抛出异常时当前线程中所有活动的方法(即进入但未退出):

    void doAndCatchErrors() {
        try {
            someMethod();
        } catch (Exception e) {
            Console.WriteLine("Error: {1}\nStack trace:\n{2}", e.ToString(), e.StackTrace.ToString());
        }
    }
};

C# 有点像 Java,因为它不支持自由函数:所有函数都必须是 class 中的方法。它们可能是静态函数 - 然后它们的行为与自由函数完全一样,但这样你就被迫将它们定位在非全局名称空间中。全局范围函数无法扩展,因为在大型项目中你会不断地与名称冲突作斗争。想象一个项目有几千个免费功能——你可以在相当简单的项目中访问那么多功能,其中一些确实有重复的名称,但每个名称都在一个单独的 class 中,因此它们不会冲突。

现在更一般的注意事项:BASIC 程序的结构和使用与良好且可读的 C# 程序相似的习语是极其罕见的。毕竟,这些是独立的编程语言,用 BASIC 表达想法的最常见、最容易理解和惯用的方式很可能在 C# 中非常笨拙并且显得陌生。即使是从 BASIC“移植”的程序,当您用 C# 重新表达它们时,也不应该真正类似于原始代码,因为两种语言的词汇在范围上有很大不同:现代 C# 比任何 BASIC 都更具表现力,并且我一点也不夸张。必须在 BASIC 中从头开始实现的数据结构和操作可能会在您将 C# 程序定位到的框架中找到(这将是现代东西的 .Net Core 框架)——所以我希望重写任何 BASIC 程序在 C# 中将更短、更易于维护、更易于理解,并且性能可能也会更好。