将数组做为Dictionary的元素问题

shijies 2020-10-15 10:36:15
将一维数组、二维数组和交错数组做为元素放入Dictionary,欲使用用自定义的FromKeyToValue函数根据键获取值,写了一段代码,但是以下代码问题不少。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
class MyArray
{
public int no;
public string[] numbers;
public string[,] names;
public string[][] scores;

public MyArray()
{
this.no = 0;
}


public MyArray(string[] numbers)
{
this.no = 1;
this.numbers = numbers;
}
public MyArray(string[,] names)
{
this.no = 2;
this.names = names;
}
public MyArray(string[][] scores)
{
this.no = 3;
this.scores = scores;
}


void FromKeyToValue(string key)
{
Dictionary<string, MyArray> Dic = new Dictionary<string, MyArray>();

string[] mf3;

string[,] siblings;

string[][] renyudi;

MyArray v1 = Dic[key];
int No = v1.no;

switch (No)
{
case 1:

foreach (string i in mf3) { System.Console.WriteLine(i); }
break;
case 2:

foreach (string i in siblings) { Console.Write("{0} ", i); }
break;
case 3:

foreach (string[] i in renyudi)
{
foreach (string j in i)
{
Console.WriteLine(j);
}
}
break;
default:
Console.WriteLine("出现错误!");
break;
}
}







class Program
{
static void Main(string[] args)
{
Dictionary<string, MyArray> Dic = new Dictionary<string, MyArray>();

string[] mf3 = { "c", "c++", "c#" };
MyArray myArray1 = new MyArray(mf3);
Dic.Add("赵", myArray1);

string[,] siblings = new string[,] { { "Mike", "Amy" }, { "Mary", "Albert" } };
MyArray myArray2 = new MyArray(siblings);
Dic.Add("钱", myArray2);

string[][] renyudi = new string[2][] { new string[] { "Mike", "Amy", "Albert" }, new string[] { "Beijing", "Shanghai", "Tianjin", "Guangzhou", "Xiamen" } };
MyArray myArray3 = new MyArray(renyudi);
Dic.Add("孙", myArray3);

MyArray.FromKeyToValue("钱");

}
}
}
}


严重性 代码 说明 项目 文件 行 禁止显示状态
错误 CS0165 使用了未赋值的局部变量“mf3” ConsoleApp1 E:\Hashmap\自定义函数\ConsoleApp1\ConsoleApp1\Program.cs 56 活动的


严重性 代码 说明 项目 文件 行 禁止显示状态
错误 CS0165 使用了未赋值的局部变量“siblings” ConsoleApp1 E:\Hashmap\自定义函数\ConsoleApp1\ConsoleApp1\Program.cs 60 活动的


严重性 代码 说明 项目 文件 行 禁止显示状态
错误 CS0165 使用了未赋值的局部变量“renyudi” ConsoleApp1 E:\Hashmap\自定义函数\ConsoleApp1\ConsoleApp1\Program.cs 64 活动的

严重性 代码 说明 项目 文件 行 禁止显示状态
错误 CS0120 对象引用对于非静态的字段、方法或属性“MyArray.FromKeyToValue(string)”是必需的 ConsoleApp1 E:\Hashmap\自定义函数\ConsoleApp1\ConsoleApp1\Program.cs 102 活动的



...全文
3040 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanghui0380 2020-10-16
  • 打赏
  • 举报
回复
void FromKeyToValue(string key) { Dictionary<string, MyArray> Dic = new Dictionary<string, MyArray>(); string[] mf3; string[,] siblings; string[][] renyudi; 自己瞅,微软并没冤枉你

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧