將一維陣列、二維陣列和交錯陣列做為元素放入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 活動的
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/173621.html
標籤:C#
上一篇:WPF開源控制元件擴展庫 - MaterialDesignExtensions
下一篇:Socket基礎
