Imagine this situation:
You have a string, and called a funtion that recieves this string as parameter and change the value inside the funtion. The string value outside the function does not change, only change inside the function.
But if you do the same with a List<string>
the List<string>
content its modified outside the function.
Why this happen?
Look up this code for a repo:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<string> list = new List<string>();
list.Add("1");
string text = "text";
ChangeSomethingInText(text);
Console.WriteLine(text);
ChangeSomethingInList(list);
foreach (var i in list)
{
Console.WriteLine(i);
}
}
public static void ChangeSomethingInText(string text)
{
text = "Text changed";
}
public static void ChangeSomethingInList(List<string> myList)
{
myList.Add("From change");
}
}
This is the result: text
is still "text", but list
has a new element.
I tested it in C# with a string vs List and in Java with a String vs an ArrayList with the same behavior.