I want to give a certain linked list to a class I am making. I want the class to write into that list (eg by .addLast()).
Should I use the ref keyword for that?
I am somewhat puzzled on where to use the ref and out keywords in C#, as all classes are allocated dynamically on the heap and we actually use pointers for most operations.
Of course, out and ref keywords make sense for primitives and structs.
Also, if I don’t send the list directly, but send a class containing the list? (it’s internal and needed), do I still need to use ref? or if I pass it between functions, ex:
void A(ref LinkedList<int> list){
B(list);
}
void B(ref LinkedList<int> list){
_myList = list;
}
For what you’re doing you don’t need to use ref. If you did pass the list using ref, you would be allowing the caller to change which list you’re referencing, rather than just changing the contents of the list.