简体   繁体   English

C#Array.Resize传递引用而不是值

[英]C# Array.Resize Passing a Ref instead of a Value

Title might be a bit misleading 标题可能有点误导

I have a string array. 我有一个字符串数组。 That I would like to pass by reference. 我想参考一下。

I know it can be accomplished simply by 我知道可以通过以下方式实现

public class test{
    string[] content = {"abc", "abd"};
    ViewContent vc = new ViewContent();

    public static void Main()
    {
        vc.InitView(content);
    }
}

public class ViewContent{
     string[] contentToView;

     public void InitView(ref string[] contentToShow)
     {
         contentToView = contentToShow;
         View();
     }

     public void View()
     {
         //Do whatever with contentToView
         //Example
         Array.Resize<string> (ref contentToView, someInt);
     }
}

If I were to resize the array with 如果我要调整数组的大小

Array.Resize()

The reference breaks and any further edits upon the resized array is not reflected on the main array from test class. 引用中断,对调整大小后的数组的任何进一步编辑都不会反映在test类的主数组上。

My question is: 我的问题是:
How do I prevent the reference to the main array from breaking when the need arises for me to resize it? 当需要调整大小时,如何防止对主数组的引用中断?
Rephrase 另一种方式
How do I resize contentToView while also resizing content in test class? 如何在调整test类的content同时调整contentToView大小?

Yes, I know it would be simpler to use Lists but I am experimenting with Arrays. 是的,我知道使用列表会更简单,但是我正在尝试使用数组。

Yes, I know my problems would be solved if I pass in the test object instead of just an array. 是的,我知道如果我通过test对象而不只是数组,就可以解决我的问题。 I am avoiding this method because I have other objects that have string arrays as well. 我避免使用此方法,因为我还有其他具有字符串数组的对象。 You might ask that if I have other objects, that serve similar functions I could use inheritance and take in the base class to ensure that all my content can be shown. 您可能会问,如果我还有其他具有类似功能的对象,可以使用继承并接受基类以确保可以显示所有内容。 This is something I would like to avoid. 这是我要避免的事情。

How do I prevent the reference to the main array from breaking when the need arises for me to resize it? 当需要调整大小时,如何防止对主数组的引用中断?

You don't. 你不知道 You just don't use an array. 您只是不使用数组。 Array objects aren't resizable, and after you've copied the value of contentToShow into contentToView , any further changes to the contentToShow variable are ignored, and likewise in reverse. 数组对象不可调整大小,将contentToShow的值复制到contentToView ,对contentToShow变量的任何进一步更改都将被忽略,反之亦然。

What you're asking is for contentToView to change its value (which is a reference) every time content in your test class changes, or vice versa. 您要的是每次test类中的content发生更改时, contentToView更改其值(作为参考),反之亦然。 There's no way of achieving that. 无法实现这一目标。 You could introduce another level of indirection with a Wrapper<T> class you'd write which basically looks like this: 您可以使用编写的Wrapper<T>类引入另一种间接级别,该类基本上是这样的:

public class Wrapper<T>
{
    public T Value { get; set; }
}

If you then made test.content and ViewContent.contentToView both Wrapper<string[]> fields, then if you change test.content.Value , you'd see that change in ViewContent.contentToView.Value ... but I would advise against it. 如果然后同时对两个Wrapper<string[]>字段都进行了test.contentViewContent.contentToView更改,则如果更改了test.content.Value ,则会在ViewContent.contentToView.Value看到该更改……但我建议不ViewContent.contentToView.Value它。

There is no way to resize array itself. 无法调整数组本身的大小。 Array.Resize method creates new array. Array.Resize方法创建新的数组。 So in anyway your old reference will not work. 因此,无论如何,您的旧参考资料将不起作用。 Also there is no sense to use ref keyword in your code. 同样,在代码中使用ref关键字也没有意义。 It can be useful only if you will reassign contentToShow inside InitView method. 仅当您将在InitView方法中重新分配contentToShow ,它InitView用。 You have to use some class ( List or your own class) if you want to "resize" your array (reassign it to the new array). 如果要“调整”数组的大小(将其重新分配给新数组),则必须使用某个类( List或您自己的类)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM