In C# should I pass a parameter by value and return the same variable, or pass by reference?

List like all reference types, is passed as a reference to the object, and not a copy of it.

Note that this is very different from saying it is passed by reference, as that would imply assignment of the parameter propagates to the caller, which it does not

It does means that modifications to the object (such as those performed by RemoveAt) will automatically propagate to the caller.

Thus, just pass it; no need for a return value or out/ref parameters.

You will very rarely use out/ref for reference types, and when used for value types, the performance difference will be so small versus returning that you shouldn't worry about it unless you have profiled and made sure that the problem occurs there. Use what makes the most idiomatic sense.


In C# the parameter are passed by value. This mean that when you pass a parameter to a method, a copy of the parameter is passed. C# have types by value (like int) and by reference (like any class). C# contains an stack (when push all varaibles) and a Heap. The value of the value types are pushing directly in this stack, while the reference of the reference type are push in the stack, and the referenced value are pushed in the Heap.
When you pass a reference type (like a List) it make a copy of the reference, but this copy point to the same object into the list. Therefore any change affect directly to the object, unless you change the reference (with an assigmet), but this is not your case.

this could by your code:

    static void DeleteCustomer<T>(List<T> customers)
    {
        Console.WriteLine("Enter ID of customer to delete: ");
        int deleteId;
        if (int.TryParse(Console.ReadLine(), out deleteId)) // if the input is an int
        {
            Console.Write("Are you sure you want to delete this customer?");
            if (Console.ReadLine().ToLower() == "y")
            {
                customers.RemoveAt(deleteId);
            }
        }
        else
        {
            Console.WriteLine("This is not valid Id");
        }
    }

If you want to know about ref an out keyword i can help you too, but for this example is not neccesary.