从php到c#:词典数组或词典词典?

时间:2019-04-06 21:54:59

标签: c# php dictionary

我是编程的新手。我有这个php代码,它基本上用于翻译txt文件中的一些句子:

$LANG = array();

$LANG['en'] = array(
    11 => "Name",
    20 => "Surname",
    21 => "Age",
    22 => "Profession",
);
$LANG['es'] = array(
    11 => "Nombre",
    20 => "Apellido",
    21 => "Edad",
    22 => "Profesión",
);

我正在尝试在c#中做同样的事情,像这样:

Dictionary<int, string>[] LANG = new Dictionary<int, string>[]
        {
        new Dictionary<int, string>(),
        LANG['en']=new Dictionary<int, string>()
             {
                 {11, "Name"},
                 {20, "Surname"},
                 {21, "Age"},
                 {22, "Profession"}
             },
        LANG['es']=new Dictionary<int, string>()
             {
                 {11, "Nombre"},
                 {20, "Apellido"},
                 {21, "Edad"},
                 {22, "Profesión"}
             }
        };

1)这个C#与php代码完全一样吗?我应该编写一系列词典,还是应该编写词典词典?

2)对于LANG['en']LANG['es'],我收到错误消息“字段初始化程序无法引用非静态字段,方法或属性'Form1.LANG'。我在做什么?错误吗?

1 个答案:

答案 0 :(得分:0)

1)是。 #include <stdio.h> #include <string.h> #include <stdlib.h> #define DISCOUNT 10 #define UNITCOST 100 #define NAMESIZE 30 #define NAMES 5 #define RESPONSE 4 #define MAXUNITS 10 #define YESNO 15 void getName(char* name); void getHousing(char* housing); void getUnits(int* units); int amountDue(char* housing, int unit); int main() { char names[NAMES][NAMESIZE]; char house[YESNO]; int units[MAXUNITS]; int amountdue[NAMES]; for (int i = 0; i < 5; i++) { getName(names[i]); getHousing(house); getUnits(units); puts(""); } for (int i = 0; i < 5; i++) { amountDue(house,units[i]); } } void getName(char* name) { printf("Enter student name: "); fgets(name,NAMESIZE,stdin); name[ strspn( name, "\n" ) ] = '\0'; } void getHousing(char* housing) { printf("Enter yes if student lives on campus, no otherwise: "); fgets(housing,RESPONSE,stdin); } void getUnits(int* units) { printf("Enter current unit count: "); scanf("%d", units); } int amountDue(char* housing, int unit) { } 中的PHP个等效数组将是一个C#Dictionary中的数组不支持非整数索引,因此您需要将C#Dictionary键一起使用,因此必须使用字典词典。

2)显然语法无效。要将您的string代码转换为PHP,请执行以下操作

C#