初学者:是否有一种优雅的方式来更新实例的所有副本?

发布于 2025-01-26 14:04:44 字数 264 浏览 3 评论 0原文

以下代码不会更新数组内部的副本。

int a = null;    
int[] numbers = new int[1];
numbers[0] = a;
a = 5; // 5
Console.WriteLine(numbers[0]); // null

获得了一个编程任务,需要设置由门户之间链接的位置结构,这仅通过列出所需的连接就不可能。即使我以后在代码中填写实体,我也会得到NULL的参考。

寻找可能解决我问题的关键字或技术。

The following code doesn't update the copy of a inside the array.

int a = null;    
int[] numbers = new int[1];
numbers[0] = a;
a = 5; // 5
Console.WriteLine(numbers[0]); // null

Got a programming task requiring to set-up a structure of locations linked by portals between them which isn't possible by just listing the required connections. I'll get references to null that stay null even if I fill an entity later in the code.

Looking for keywords or techniques which might solve my issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清欢 2025-02-02 14:04:44

您可以具有参考类型而不是数组中的值类型,因此更新内在对象的值也会影响数组。

var tab = new MyClass[1];
var obj = new MyClass(5);
tab[0] = obj;
Console.WriteLine(tab[0].Value);  // 5

tab[0].Value = 10;
Console.WriteLine(tab[0].Value);  // 10

obj.Value = 15;
Console.WriteLine(tab[0].Value);  // 15

public class MyClass
{
    public MyClass(int value)
    {
        Value = value;
    }
    public int Value { get; set; }
}

You could have reference types instead of value types inside array, therefore updating the value of the inner object will also affect the array.

var tab = new MyClass[1];
var obj = new MyClass(5);
tab[0] = obj;
Console.WriteLine(tab[0].Value);  // 5

tab[0].Value = 10;
Console.WriteLine(tab[0].Value);  // 10

obj.Value = 15;
Console.WriteLine(tab[0].Value);  // 15

public class MyClass
{
    public MyClass(int value)
    {
        Value = value;
    }
    public int Value { get; set; }
}
静待花开 2025-02-02 14:04:44

整数为a ,因此复制了实际值。因此,您的示例代码中从未有任何“实例”,只有该值的副本。

您可能应该将您的价值包裹在一堂课中,因为类是A 参考类型以获得所需的行为。当您需要在多个组件之间共享一些可变的时,这可能会很有用。您还可以添加一个事件,每当更改值以使需要值的任何组件都知道它可能需要更新某些内容时,该事件会添加。例如:

    public class MyChangeable<T>
    {
        private T value;
        public MyChangeable(T value) => this.value = value;
        public T Value
        {
            get => value;
            set
            {
                this.value = value;
                OnChanged(this, value);
            }
        }
        public event EventHandler<T> OnChanged;
    }

也有裁判返回和参考本地可以做类似您的示例的事情,但这主要是为了避免大型结构的副本来获得更好的性能,如果要在组件之间共享值,这将不那么有用。

integers is a value type, as such the actual value is copied. So there is never any 'instance' in your example code, only copies of the value.

You should probably wrap your value in a class, since classes are a reference type to get your desired behavior. This might be useful when you need to share some mutable between multiple components. You can also add an event that is raised whenever the value is changed to let any component that needs the value know that it might need to update something. For example:

    public class MyChangeable<T>
    {
        private T value;
        public MyChangeable(T value) => this.value = value;
        public T Value
        {
            get => value;
            set
            {
                this.value = value;
                OnChanged(this, value);
            }
        }
        public event EventHandler<T> OnChanged;
    }

There is also ref return and ref locals that could do something like your example, but this is mostly intended to get better performance by avoiding copies of large structs, it is not as useful if you want to share values between components.

亢潮 2025-02-02 14:04:44

数组是参考类型,

var a = new int[1];    
var numbers = new [] { a };

a[0] = 5;
Console.WriteLine(numbers[0][0]); 

您只需要记住,您比您想要的要深度更深。这有点有些黑客,我可能会像其他答案一样为它上课,但是在大小1的数组中填充值类型可能是一种有用的技术,可以快速从价值类型中获得参考类型的行为

Arrays are reference types

var a = new int[1];    
var numbers = new [] { a };

a[0] = 5;
Console.WriteLine(numbers[0][0]); 

You just have to remember that you're one level deeper than you wanted to be/you need to stick a [0] on everything you wouldn't have stuck it on before. It's a bit of a hack, and I'd probably make a class for it like other answers recommend, but stuffing a value type in an array of size 1 can be a useful technique to quickly get reference type behavior out of a value type

○愚か者の日 2025-02-02 14:04:44

当地人即使由于涉及对象的寿命而受到严格的限制,他们可能会帮助您完成任务。

基于您的问题的一个小示例可以如下:

int[] array = new int[1];
ref int elem = ref array[0];
elem = 5;
Console.WriteLine(array[0]); // 5

这不仅可以使用价值类型(包括无效类型),还可以与参考类型一起使用。

ref locals may help you in your task, even if they have strict limitations due to the lifetime of the involved objects, so they could be not applicable as a general solution.

A small example based on your question can be as follows:

int[] array = new int[1];
ref int elem = ref array[0];
elem = 5;
Console.WriteLine(array[0]); // 5

This works not only with value types (including nullable types) but also with reference types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文