C# 在C语言中,向字典中的特定键添加多个值#
我有6本字典,根据盘子、碗或勺子的情况保存键1、值1和键1、值2 首先,我在不同的字典中保存了不同的值,但现在我想将与图版相关的值保存为name=“plate1”,例如:PlateID、color和key的条件值(100) 类似地,对于碗和汤匙,我想删除最后3个字典,只使用前3个字典来保存这些值 类的新实现:C# 在C语言中,向字典中的特定键添加多个值#,c#,xml,dictionary,C#,Xml,Dictionary,我有6本字典,根据盘子、碗或勺子的情况保存键1、值1和键1、值2 首先,我在不同的字典中保存了不同的值,但现在我想将与图版相关的值保存为name=“plate1”,例如:PlateID、color和key的条件值(100) 类似地,对于碗和汤匙,我想删除最后3个字典,只使用前3个字典来保存这些值 类的新实现: abstract class Silver { public double SilverId { get; set; } public double InnerTextVa
abstract class Silver
{
public double SilverId { get; set; }
public double InnerTextValue { get; set; }
public double Color { get; set; }
}
class PlateValues : Silver{ }
class BowlValues : Silver { }
class SpoonValues : Silver{ }
private static IDictionary<string, Silver> plateValues = new Dictionary<string, Silver>();
private static IDictionary<string, Silver> bowlValues = new Dictionary<string, Silver>();
private static IDictionary<string, Silver> spoonValues = new Dictionary<string, Silver>();
<?xml version="1.0" encoding="utf-8"?>
<crockery version="2" lastmodified="2015-08-06 03:53:06.207">
<silver>
<silvertype silverid="8504p" >
<attributes>
<attribute name="plate1" type="int">
<conditions>
<condition type="shine" color="White">100</condition>
</conditions>
</attribute>
<attribute name="plate4" type="int">
<conditions>
<condition type="shine" color="gold">87</condition>
</conditions>
</attribute>
</attributes>
</silvertype>
<silvertype silverid="3487s" >
<attributes>
<attribute name="spoon2" type="int">
<conditions>
<condition type="shine" color="black">58</condition>
</conditions>
</attribute>
<attribute name="spoon6" type="int">
<conditions>
<condition type="shine" color="gold">45</condition>
</conditions>
</attribute>
</attributes>
</silvertype>
<silvertype silverid="2398b" >
<attributes>
<attribute name="bowl7" type="int">
<conditions>
<condition type="shine" color="black">8</condition>
</conditions>
</attribute>
<attribute name="bowl4" type="int">
<conditions>
<condition type="shine" color="gold">45</condition>
</conditions>
</attribute>
</attributes>
</silvertype>
</silver>
</crockery>
字典:
abstract class Silver
{
public double SilverId { get; set; }
public double InnerTextValue { get; set; }
public double Color { get; set; }
}
class PlateValues : Silver{ }
class BowlValues : Silver { }
class SpoonValues : Silver{ }
private static IDictionary<string, Silver> plateValues = new Dictionary<string, Silver>();
private static IDictionary<string, Silver> bowlValues = new Dictionary<string, Silver>();
private static IDictionary<string, Silver> spoonValues = new Dictionary<string, Silver>();
<?xml version="1.0" encoding="utf-8"?>
<crockery version="2" lastmodified="2015-08-06 03:53:06.207">
<silver>
<silvertype silverid="8504p" >
<attributes>
<attribute name="plate1" type="int">
<conditions>
<condition type="shine" color="White">100</condition>
</conditions>
</attribute>
<attribute name="plate4" type="int">
<conditions>
<condition type="shine" color="gold">87</condition>
</conditions>
</attribute>
</attributes>
</silvertype>
<silvertype silverid="3487s" >
<attributes>
<attribute name="spoon2" type="int">
<conditions>
<condition type="shine" color="black">58</condition>
</conditions>
</attribute>
<attribute name="spoon6" type="int">
<conditions>
<condition type="shine" color="gold">45</condition>
</conditions>
</attribute>
</attributes>
</silvertype>
<silvertype silverid="2398b" >
<attributes>
<attribute name="bowl7" type="int">
<conditions>
<condition type="shine" color="black">8</condition>
</conditions>
</attribute>
<attribute name="bowl4" type="int">
<conditions>
<condition type="shine" color="gold">45</condition>
</conditions>
</attribute>
</attributes>
</silvertype>
</silver>
</crockery>
示例XML文件:
abstract class Silver
{
public double SilverId { get; set; }
public double InnerTextValue { get; set; }
public double Color { get; set; }
}
class PlateValues : Silver{ }
class BowlValues : Silver { }
class SpoonValues : Silver{ }
private static IDictionary<string, Silver> plateValues = new Dictionary<string, Silver>();
private static IDictionary<string, Silver> bowlValues = new Dictionary<string, Silver>();
private static IDictionary<string, Silver> spoonValues = new Dictionary<string, Silver>();
<?xml version="1.0" encoding="utf-8"?>
<crockery version="2" lastmodified="2015-08-06 03:53:06.207">
<silver>
<silvertype silverid="8504p" >
<attributes>
<attribute name="plate1" type="int">
<conditions>
<condition type="shine" color="White">100</condition>
</conditions>
</attribute>
<attribute name="plate4" type="int">
<conditions>
<condition type="shine" color="gold">87</condition>
</conditions>
</attribute>
</attributes>
</silvertype>
<silvertype silverid="3487s" >
<attributes>
<attribute name="spoon2" type="int">
<conditions>
<condition type="shine" color="black">58</condition>
</conditions>
</attribute>
<attribute name="spoon6" type="int">
<conditions>
<condition type="shine" color="gold">45</condition>
</conditions>
</attribute>
</attributes>
</silvertype>
<silvertype silverid="2398b" >
<attributes>
<attribute name="bowl7" type="int">
<conditions>
<condition type="shine" color="black">8</condition>
</conditions>
</attribute>
<attribute name="bowl4" type="int">
<conditions>
<condition type="shine" color="gold">45</condition>
</conditions>
</attribute>
</attributes>
</silvertype>
</silver>
</crockery>
我是否继续出错?您可以使用结构作为值
struct myStruct
{
int value1, value2;
string value3;
}
Dictionary<string,myStruct> myDictionary = new Dictionary<string,myStruct>();
myDictionary.Add("Key", new myStruct() {value1=12, ...});
struct myStruct
{
int值1、值2;
字符串值3;
}
Dictionary myDictionary=新字典();
Add(“Key”,newmystruct(){value1=12,…});
您可以使用结构作为值
struct myStruct
{
int value1, value2;
string value3;
}
Dictionary<string,myStruct> myDictionary = new Dictionary<string,myStruct>();
myDictionary.Add("Key", new myStruct() {value1=12, ...});
struct myStruct
{
int值1、值2;
字符串值3;
}
Dictionary myDictionary=新字典();
Add(“Key”,newmystruct(){value1=12,…});
您可以使用结构作为值
struct myStruct
{
int value1, value2;
string value3;
}
Dictionary<string,myStruct> myDictionary = new Dictionary<string,myStruct>();
myDictionary.Add("Key", new myStruct() {value1=12, ...});
struct myStruct
{
int值1、值2;
字符串值3;
}
Dictionary myDictionary=新字典();
Add(“Key”,newmystruct(){value1=12,…});
您可以使用结构作为值
struct myStruct
{
int value1, value2;
string value3;
}
Dictionary<string,myStruct> myDictionary = new Dictionary<string,myStruct>();
myDictionary.Add("Key", new myStruct() {value1=12, ...});
struct myStruct
{
int值1、值2;
字符串值3;
}
Dictionary myDictionary=新字典();
Add(“Key”,newmystruct(){value1=12,…});
有几种方法可以做到这一点,但我会建立一个OOP层次结构。这允许您以后添加特定于勺子的属性或方法:
abstract class Silver
{
public double Id {get; set;}
public double Value {get; set;}
}
class Plate : Silver {}
class Spoon : Silver {}
class Bowl : Silver {}
而且,由于您的名字看起来都是唯一的,所以您只需要一本字典:
Dictionary<string, Silver> silvers = new Dictionary<string, Silver>();
然后,例如,如果您只想枚举图版,可以使用IEnumerable.OfType
:
enum SilverType { Plate, Spoon, Bowl }
class Silver
{
public double Id {get; set;}
public double Value {get; set;}
public SilverType SilverType {get; set;}
}
有几种方法可以做到这一点,但我会建立一个面向对象的层次结构。这允许您以后添加特定于勺子的属性或方法:
abstract class Silver
{
public double Id {get; set;}
public double Value {get; set;}
}
class Plate : Silver {}
class Spoon : Silver {}
class Bowl : Silver {}
而且,由于您的名字看起来都是唯一的,所以您只需要一本字典:
Dictionary<string, Silver> silvers = new Dictionary<string, Silver>();
然后,例如,如果您只想枚举图版,可以使用IEnumerable.OfType
:
enum SilverType { Plate, Spoon, Bowl }
class Silver
{
public double Id {get; set;}
public double Value {get; set;}
public SilverType SilverType {get; set;}
}
有几种方法可以做到这一点,但我会建立一个面向对象的层次结构。这允许您以后添加特定于勺子的属性或方法:
abstract class Silver
{
public double Id {get; set;}
public double Value {get; set;}
}
class Plate : Silver {}
class Spoon : Silver {}
class Bowl : Silver {}
而且,由于您的名字看起来都是唯一的,所以您只需要一本字典:
Dictionary<string, Silver> silvers = new Dictionary<string, Silver>();
然后,例如,如果您只想枚举图版,可以使用IEnumerable.OfType
:
enum SilverType { Plate, Spoon, Bowl }
class Silver
{
public double Id {get; set;}
public double Value {get; set;}
public SilverType SilverType {get; set;}
}
有几种方法可以做到这一点,但我会建立一个面向对象的层次结构。这允许您以后添加特定于勺子的属性或方法:
abstract class Silver
{
public double Id {get; set;}
public double Value {get; set;}
}
class Plate : Silver {}
class Spoon : Silver {}
class Bowl : Silver {}
而且,由于您的名字看起来都是唯一的,所以您只需要一本字典:
Dictionary<string, Silver> silvers = new Dictionary<string, Silver>();
然后,例如,如果您只想枚举图版,可以使用IEnumerable.OfType
:
enum SilverType { Plate, Spoon, Bowl }
class Silver
{
public double Id {get; set;}
public double Value {get; set;}
public SilverType SilverType {get; set;}
}
您是否考虑过创建一个包含所有值的类型,然后将其添加到DICITORY中?“那么像字典之类的东西呢?”BoasEnkler说,是的,但我不知道该如何实现。我见过元组和键的例子,但是类1似乎更好。你有没有考虑过创建一个包含所有值的类型,然后将其添加到dicitonary中?“那么像字典之类的东西呢?”BoasEnkler说,是的,但我不知道该如何实现。我见过元组和键的例子,但是类1似乎更好。你有没有考虑过创建一个包含所有值的类型,然后将其添加到dicitonary中?“那么像字典之类的东西呢?”BoasEnkler说,是的,但我不知道该如何实现。我见过元组和键的例子,但是类1似乎更好。你有没有考虑过创建一个包含所有值的类型,然后将其添加到dicitonary中?“那么像字典之类的东西呢?”BoasEnkler说,是的,但我不知道该如何实现。我见过元组和键的例子,但是类1似乎更好。你也可以使用类,这取决于它的复杂程度。两者都可以:)虽然这是绝对正确的解决方案,但我不建议任何人从
struct
开始。对于许多人来说,struct(尤其是示例中的可变结构)的行为是无法理解的,并且会导致不必要的问题(在这种情况下,使用struct
是否有任何好处尚不清楚)。@AlexeiLevenkov如果他只是使用它将数据保存在一起,并且可以通过一个键访问,我认为他在使用struct
时应该不会有任何问题。你也可以使用类,这取决于它的复杂程度。两者都可以:)虽然这是绝对正确的解决方案,但我不建议任何人从struct
开始。对于许多人来说,struct(尤其是示例中的可变结构)的行为是无法理解的,并且会导致不必要的问题(在这种情况下,使用struct
是否有任何好处尚不清楚)。@AlexeiLevenkov如果他只是使用它将数据保存在一起,并且可以通过一个键访问,我认为他在使用struct
时应该不会有任何问题。你也可以使用类,这取决于它的复杂程度。两者都可以:)虽然这是绝对正确的解决方案,但我不建议任何人从struct
开始。对于许多人来说,struct(尤其是示例中的可变结构)的行为是无法理解的,并且会导致不必要的问题(在这种情况下,使用struct
是否有任何好处尚不清楚)。@AlexeiLevenkov如果他只是使用它将数据保存在一起,并且可以通过一个键访问,我认为他在使用struct
时应该不会有任何问题。你也可以使用类,这取决于它的复杂程度。两者都可以:)虽然绝对正确的解决方案,但我不推荐任何人