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.

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?

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. 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.

Jon Skeet
people
quotationmark

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.

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. 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:

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.

people

See more on this question at Stackoverflow