相关疑难解决方法(0)

如何在 C# 中使用两个键创建字典?

在 C# 中,我想要一个将 (x,y) 坐标映射到 (x,y) 的数据结构。我怎样才能做这样的事情?

我不想使用类似 的公式将 x,y 坐标转换为单个值y*w+x。有没有办法可以拥有dictionary<key,key,(value,value)>.

如果我将键作为 Tuple,那么它是一个对象,并且 Tuple(1,1) 不等于 Tuple(1,1)。所以我认为我无法找到这个意义上的钥匙。

c# dictionary

3
推荐指数
1
解决办法
3741
查看次数

为什么Bytes残留?

我最近一直在玩一些字节数组(处理灰度图像).一个字节的值可以是0-255.我正在修改字节,并遇到了我分配给字节的值超出字节范围的情况.它正在为我正在玩的图像做出意想不到的事情.

我写了一个测试,并了解到该字节仍然存在.例:

private static int SetByte(int y)
{
    return y;
}
.....
byte x = (byte) SetByte(-4);
Console.WriteLine(x);
//output is 252
Run Code Online (Sandbox Code Playgroud)

有一个结转!当我们走另一条路时,就会发生这种情况.

byte x = (byte) SetByte(259);
Console.WriteLine(x);
//output is 3
Run Code Online (Sandbox Code Playgroud)

我希望它在第一种情况下将其设置为255,在第二种情况下将其设置为0.这个结转的目的是什么?是不是因为我正在投射这个整数赋值?什么时候在现实世界中有用?

c#

3
推荐指数
2
解决办法
179
查看次数

根据一对字符串生成唯一的字符串

我有两个字符串 StringA、StringB。我想生成一个唯一的字符串来表示这一对。

IE

f(x, y) 对于每个 x、y 和 f(x, y) = f(y, x) 应该是唯一的,其中 x、y 是字符串。

有任何想法吗?

c# string unique

2
推荐指数
1
解决办法
3012
查看次数

如何使用自定义泛型类型作为字典中的键?

我想使用自定义泛型类作为字典中的键.我应该如何重写EqualsGetHashCode

例如,

public class SomeKey<T,V>
{
    public T Value1 { get; set; }
    public V Value2 { get; set; }

    public SomeKey(T val1, V val2)
    {
        this.Value1 = val1;
        this.Value2 = val2;
    }

    public override bool Equals(SomeKey<T,V> otherKey)
    {
        //whats the best option here?
    }

    public override int GetHashCode()
    {
        //whats the best option here?
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢

c#

2
推荐指数
1
解决办法
279
查看次数

我应该何时在不可变结构上实现GetHashCode?

我在这里读到了一些与GetHashCode正确实施相关的问题.我没找到的是什么时候应该实现这个方法.

在我的具体情况下,我构建了一个简单的不可变结构:

public struct MyStruct
{
    private readonly Guid m_X;
    private readonly string m_Y;
    private readonly string m_Z;

    public Guid string X
    {
        get { return m_X; }
    }

    public string Y
    {
        get { return m_Y; }
    }

    public string Z
    {
        get { return m_Z; }
    }

    public MyStruct(Guid x, string y, string z)
    {
        if (x == Guid.Empty) throw new ArgumentException("x cannot be equals to Guid.Empty", "x");
        if (string.IsNullOrEmpty(y)) throw new ArgumentException("y cannot …
Run Code Online (Sandbox Code Playgroud)

c#

2
推荐指数
1
解决办法
268
查看次数

来自对象列表的不同值

我需要你的帮助.我试图从对象列表中获取不同的值.我的班级看起来像这样:

class Chromosome
{
    public bool[][] body { get; set; }
    public double fitness { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在我有List<Chromosome> population.现在我需要的是一种方式,我如何获得新的清单:List<Chromosome> newGeneration.这个新列表将只包含原始列表中的唯一染色体 - 种群.

染色体是独特的,当他的整个身体(在这种情况下是2D bool阵列)与其他染色体相比是独特的. 我知道,有类似MoreLINQ,但我不知道,我是否应该使用第三方代码,我知道我应该覆盖一些方法,但我有种丢失.所以我真的很感激一些好的一步一步的描述,甚至白痴都可以完成:) THX

c# linq distinct-values

2
推荐指数
1
解决办法
1090
查看次数

避免在数据库中添加重复项的最佳方法

我有一个包含三列的SQL Server表:

表格1

col1 int
col2 int
col3 string
Run Code Online (Sandbox Code Playgroud)

我为所有三列定义了一个唯一约束 (col1, col2, col3)

现在,我有一个.csv文件,我想在此表中添加记录,*.csv文件可以有重复记录.

我在上面的场景中搜索了各种避免重复的选项.以下是适合我的三个选项.请看一下并提出一些关于每种方法的优点/缺点的想法,以便我可以选择最好的方法.

选项1 :

首先避免重复,即从csv文件向列表添加对象时.我用过HashSet<T>了这个并覆盖了类型T下面的方法:

public override int GetHashCode()
{
    return col1.GetHashCode() + col2.GetHashCode() + col3.GetHashCode();
}

public override bool Equals(object obj)
{
    var other = obj as T;
    if (other == null)
    {
        return false;
    }
    return col1 == other.col1
        && col2 == other.col2
        && col3 == other.col3;
}
Run Code Online (Sandbox Code Playgroud)

选项#2

List<T>代替HashSet<T>.

添加所有对象后删除重复项 List<T>

    List<T> distinctObjects = allObjects
        .GroupBy(x => new …
Run Code Online (Sandbox Code Playgroud)

c# sql-server hashset

2
推荐指数
1
解决办法
1684
查看次数

c#包含不工作的对象

我正在尝试在列表之间创建一个检查,但我没有运气:-/

我有一个包含 100 个字段的游戏板,并进行此循环以仅将空字段添加到新列表中:

for(int i = 0; i < thisGame.boardFields.Count; i++)
{
    if (thisGame.boardFields.Count != 0 && thisGame.boardFields [i] != null) 
    {
        BoardField thisField = thisGame.boardFields [i];
        if (thisField.owner == "0" && thisField.number != 13) 
        {
            Tile tTile = new Tile();
            tTile.color = thisField.color;
            tTile.number = thisField.number.ToString();

            tempBoard.Add (tTile);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我遍历玩家 5 块瓷砖,看看玩家是否有一块不可玩的瓷砖,例如,具有相同对象的空字段不可用,如下所示:

for (var i = 0; i < thisGame.playerTiles.Count; i++)
{    
    Tile tempTile = new Tile();
    tempTile.color = thisGame.playerTiles[i].color;
    tempTile.number = thisGame.playerTiles[i].number.ToString();

    if (!tempBoard.Contains (tempTile)) …
Run Code Online (Sandbox Code Playgroud)

c# contains list

2
推荐指数
1
解决办法
7556
查看次数

C#Dictionary中的KeyNotFoundException在根据内容更改属性值后,计算GetHashCode.为什么?

请参阅下面的代码.

            static void Main(string[] args)
            {
    // Create Dictionary
                var dict = new Dictionary<TestClass, ValueClass>();

    // Add data to dictionary
                CreateSomeData(dict); 

    // Create a List
                var list = new List<TestClass>();
                foreach(var kv in dict) {
    // Swap property values for each Key
    // For example Key with property value 1 will become 6
    // and 6 will become 1
                    kv.Key.MyProperty = 6 - kv.Key.MyProperty + 1;

    // Add the Key to the List
                    list.Add(kv.Key);
                }

// Try to print dictionary …
Run Code Online (Sandbox Code Playgroud)

c# hash dictionary

2
推荐指数
1
解决办法
257
查看次数

是否存在指定的(子)索引分隔符?

背景

在 PowerShell 中构建哈希表以通过特定属性快速访问对象是很常见的,例如基于以下内容建立索引LastName

$List =  ConvertFrom-Csv @'
Id, LastName, FirstName, Country
 1, Aerts,    Ronald,    Belgium
 2, Berg,     Ashly,     Germany
 3, Cook,     James,     England
 4, Duval,    Frank,     France
 5, Lyberg,   Ash,       England
 6, Fischer,  Adam,      Germany
'@

$Index = @{}
$List |ForEach-Object { $Index[$_.LastName] = $_ }

$Index.Cook

Id LastName FirstName Country
-- -------- --------- -------
3  Cook     James     England
Run Code Online (Sandbox Code Playgroud)

在某些情况下,需要在两个(甚至更多)属性上构建索引,例如 theFirstName和 the LastName。为此,您可以创建一个多维密钥,例如:

$Index = @{}
$List |ForEach-Object {
     $Index[$_.FirstName] = @{} …
Run Code Online (Sandbox Code Playgroud)

.net indexing powershell hashtable

2
推荐指数
1
解决办法
291
查看次数