比较 2 个字符串 Class 列出 C#
Comparing 2 string Class Lists C#
我有一个 GinList class,我想比较 class
的 2 个字符串列表
public class GinList
{
public string GinName;
public string Country;
public string Abv;
public string Price;
public string Votes;
public string Stars;
public GinList(string ginName, string country, string abv, string price, string votes, string stars )
{
GinName = ginName;
Country = country;
Abv = abv;
Price = price;
Votes = votes;
Stars = stars;
}
}
然后我填充杜松子酒列表 ..
public class TestScript : MonoBehaviour
{
public string ginString;
private List< GinList > sortedResults = new List< GinList >();
private List< GinList > gins = new List< GinList >();
private string[] splitGinString;
void Start ()
{
splitGinString = ginString.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
// build the new GinList()
for (var i = 0; i < splitGinString.Length-1; i++)
{
gins.Add(new GinList(splitGinString[i], splitGinString[i+1], splitGinString[i+2],
splitGinString[i+3], splitGinString[i+4], splitGinString[i+5]));
i+=6;
}
}
}
然后调试显示..
0 是 .. 5th Fire - 西班牙 - 42.00% - £3.80 - 0 - 0
1 是 .. Biercee - 比利时 - 44.00% - 4.30 英镑 - 0 - 0
2 是 .. 布莱克伍德 - 苏格兰 - 40.00% - £3.60 - 0 - 0
3 是 .. Blind Tiger - Piper Cubeba - 比利时 - 47.00% - £4.00 - 0 - 0
4 是 .. Bloom - 英格兰 - 42.00% - £3.50 - 0 - 0
等..
然后我再做一个清单,杜松子酒少了一些,然后我都试了..
sortedResults = gins.Except( Gins ).ToList();
sortedResults = Gins.Except( gins ).ToList();
然后一个..
foreach(GinList gin in sortedResults)
{
Debug.Log("Sorted Gin number " + nextPos + " is .. " + gin.GinName + " - " + gin.Country + " - "
+ gin.Abv + " - " + gin.Price + " - " + gin.Votes + " - " + gin.Stars);
nextPos++;
}
但是调试显示完整列表,我做错了什么?谢谢。
你应该阅读 documentation for Except:
The default equality comparer, Default, is used to compare values of the types that implement the IEqualityComparer generic interface. To compare a custom data type, you need to implement this interface and provide your own GetHashCode and Equals methods for the type.
简而言之,您需要在您的类型上实现相等方法。
旁注
附带说明一下,您应该不创建字段public,而是使用属性来封装字段。您可以使用自动属性为您完成大部分工作,因此您不需要字段 + 属性。如果永远不要将 setter 设置在 class 之外,您还可以将 setter 指定为 private
。如果 set
未在构造函数外部设置,则可以将其全部省略 并且 您使用的是 c# 6.0 或更高版本。
另外如前所述,您应该遵循通用的命名约定。对于属性,请使用 pascal 大小写。
示例:字段 _ginName
将变为 属性 GinName
。
public string GinName { get; set; }
我有一个 GinList class,我想比较 class
的 2 个字符串列表public class GinList
{
public string GinName;
public string Country;
public string Abv;
public string Price;
public string Votes;
public string Stars;
public GinList(string ginName, string country, string abv, string price, string votes, string stars )
{
GinName = ginName;
Country = country;
Abv = abv;
Price = price;
Votes = votes;
Stars = stars;
}
}
然后我填充杜松子酒列表 ..
public class TestScript : MonoBehaviour
{
public string ginString;
private List< GinList > sortedResults = new List< GinList >();
private List< GinList > gins = new List< GinList >();
private string[] splitGinString;
void Start ()
{
splitGinString = ginString.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
// build the new GinList()
for (var i = 0; i < splitGinString.Length-1; i++)
{
gins.Add(new GinList(splitGinString[i], splitGinString[i+1], splitGinString[i+2],
splitGinString[i+3], splitGinString[i+4], splitGinString[i+5]));
i+=6;
}
}
}
然后调试显示..
0 是 .. 5th Fire - 西班牙 - 42.00% - £3.80 - 0 - 0
1 是 .. Biercee - 比利时 - 44.00% - 4.30 英镑 - 0 - 0
2 是 .. 布莱克伍德 - 苏格兰 - 40.00% - £3.60 - 0 - 0
3 是 .. Blind Tiger - Piper Cubeba - 比利时 - 47.00% - £4.00 - 0 - 0
4 是 .. Bloom - 英格兰 - 42.00% - £3.50 - 0 - 0
等..
然后我再做一个清单,杜松子酒少了一些,然后我都试了..
sortedResults = gins.Except( Gins ).ToList();
sortedResults = Gins.Except( gins ).ToList();
然后一个..
foreach(GinList gin in sortedResults)
{
Debug.Log("Sorted Gin number " + nextPos + " is .. " + gin.GinName + " - " + gin.Country + " - "
+ gin.Abv + " - " + gin.Price + " - " + gin.Votes + " - " + gin.Stars);
nextPos++;
}
但是调试显示完整列表,我做错了什么?谢谢。
你应该阅读 documentation for Except:
The default equality comparer, Default, is used to compare values of the types that implement the IEqualityComparer generic interface. To compare a custom data type, you need to implement this interface and provide your own GetHashCode and Equals methods for the type.
简而言之,您需要在您的类型上实现相等方法。
旁注
附带说明一下,您应该不创建字段public,而是使用属性来封装字段。您可以使用自动属性为您完成大部分工作,因此您不需要字段 + 属性。如果永远不要将 setter 设置在 class 之外,您还可以将 setter 指定为 private
。如果 set
未在构造函数外部设置,则可以将其全部省略 并且 您使用的是 c# 6.0 或更高版本。
另外如前所述,您应该遵循通用的命名约定。对于属性,请使用 pascal 大小写。
示例:字段 _ginName
将变为 属性 GinName
。
public string GinName { get; set; }