将数组做为Dictionary的元素问题
将一维数组、二维数组和交错数组做为元素放入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 活动的