Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<T> - do I pass objects or references?

Tags:

c#

list

generics

Well I have looked into generics and have following question:

List<someClass> list=new List<someClass>

SomeClass MyInstance=SomeClass();

list.Add(MyInstance);

I am not sure what will be added to list - reference or object of reference type (pointing to actual value of MyInstance).
EDIT: Or I will add value (that is reference data type) which points to actual object?

Thanks

like image 557
Lojol Avatar asked Jan 19 '11 21:01

Lojol


1 Answers

When you deal with reference types you are always dealing with references, so a reference will be added to the list (a copy of the reference actually). You don't actually have a choice; that's how the language works.

like image 113
Ed S. Avatar answered Sep 29 '22 10:09

Ed S.