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.