gpt4 book ai didi

c# - ValueTuple 结构是否只能作为变量可变?

转载 作者:太空狗 更新时间:2023-10-30 00:48:44 27 4
gpt4 key购买 nike

我尝试使用 ValueTuple 结构并尝试实现一个不可变的复合键。键由值类型组成。

我试图通过一些单元测试来破坏以下实现,但到目前为止没有成功。我错过了什么吗?

此外,这只是出于好奇,我想了解 ValueTuple 及其在 .NET 4.7 发布之前的局限性。

到目前为止,我对 ValueTuple 的理解是,它仅作为变量可变,而不是作为字段或属性可变。虽然不确定这里的“可变”是什么意思。是否改变 ValueTuple 实例实际上创建了一个新的ValueTuple(就像众所周知字符串是“不可变的”但实际上 引用类型)?

from this answer

System.ValueTuple isn't only a struct, it's a mutable one, and one has to be careful when using them as such. Think what happens when a class holds a System.ValueTuple as a field.

这里是我的实现和测试

public interface IHaveCompositeKey
{
(Guid id, string name) Key { get; }
}

public class ImmutableKey : IHaveCompositeKey
{
public (Guid id, string name) Key { get; }
public ImmutableKey((Guid id, string name) key) => Key = key;
public override int GetHashCode() => Key.GetHashCode();
public override bool Equals(object obj)
{
var a = obj as ImmutableKey;
return a != null && Key.Equals(a.Key);
}
}

[TestFixture]
public class KeyTests
{
[Test]
public void Test1() // passes
{
var key = (Guid.NewGuid(), "Foo");
var a = new ImmutableKey(key);
var b = new ImmutableKey(key);
Assert.IsTrue(a.Equals(b));
Assert.IsTrue(a.GetHashCode().Equals(b.GetHashCode()));
}

[Test]
public void Test2() // passes
{
(Guid id, string name) key = (Guid.NewGuid(), "Foo");
var a = new ImmutableKey(key);
key.name = "Bar"; // mutable
var b = new ImmutableKey(key);
Assert.IsFalse(a.Equals(b));
Assert.IsFalse(a.GetHashCode().Equals(b.GetHashCode()));
}

[Test]
public void Test3() // does not compile
{
var key = (Guid.NewGuid(), "Foo");
var a = new ImmutableKey(key);
// compilation error
a.Key.name = "Bar"; // immutable
var b = new ImmutableKey(a.Key);
Assert.IsFalse(a.Equals(b));
Assert.IsFalse(a.GetHashCode().Equals(b.GetHashCode()));
}
}

error: Cannot modify the return value of 'ImmutableKey.Key' because it is not a variable

最佳答案

可以在三种情况下更改可变结构并查看结果:

  • 局部变量:MyStruct s = new MyStruct(); s.Prop = 4;
  • 另一种类型的
  • 字段:class MyType { public MyStruct S;} ... myType.S.Prop = 4;
  • 数组的元素:MyStruct[] myArray =...; myArray[3].Prop = 4; .

为什么帖子中的代码没有检测到更改 - 代码使用了属性 而不是字段。

请注意 List<MyStruct>不允许修改,因为索引器 ( this[..]) 返回项目的副本(因为不支持像 C++ 那样返回引用)。

关于c# - ValueTuple 结构是否只能作为变量可变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43852501/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com