가능한 중복 : 아래의 코드와
passing in object by ref심판에 의해 목록 <int> 전달
는, 출력은 다음과 같습니다
Without:
With:1
코드 :
static void Main(string[] args)
{
var listWithoutRef = new List<int>();
WithoutRef(listWithoutRef);
Console.WriteLine("Without:" + string.Join(" ", listWithoutRef));
var listWithRef = new List<int>();
WithRef(ref listWithRef);
Console.WriteLine("With:" + string.Join(" ", listWithRef));
}
static void WithoutRef(List<int> inList)
{
inList = new List<int>(new int[] { 1 });
}
static void WithRef(ref List<int> inList)
{
inList = new List<int>(new int[] { 1 });
}
그냥 이것을 보면서 목록이 힙에 있다고 말했을 것이므로 어쨌든 ref로 전달되므로 동일한 값을 가져야합니다. 내가 ref 키워드를 오해하고 있습니까? 아니면 다른 것을 놓치고 있습니까?
가능한 복제본 [ref by object] (http://stackoverflow.com/questions/729526/passing-in-object-by-ref) - Jon Skeet의 질문에 대한 답변은 귀하가 객체를 변경하지 않고 새로운 객체를 지정하십시오. 또한 http://stackoverflow.com/questions/635915/when-to-use-ref-and-when-it-is-not-necessary-in-c-sharp?rq=1 –