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

In a C # program, I created a method that removes an object from a list. The user enters the index of the item to delete, then the user is prompted to confirm the removal, and the item is removed from the list if the user confirms, otherwise the list remains unchanged.
I am not sure of the best way to pass arguments to a method. I tried passing the list by reference (as an out parameter):

 static void DeleteCustomer(out List<Customer> customers) { // ...display list of objects for user to choose from... int deleteId = ReadInt("Enter ID of customer to delete: "); Console.Write("Are you sure you want to delete this customer?"); if (Console.ReadLine().ToLower() == "y") { customers.RemoveAt(deleteId); } } 

The above code does not work as I am getting errors. The use of the unrecognized local variable clients and the out parameter for the out parameter must be assigned before the control leaves the current method. I thought I could pass the list by value and return the same list, for example:

 static List<Customer> DeleteCustomer(List<Customer> customers) { int deleteId = ReadInt("Enter ID of customer to delete: "); Console.Write("Are you sure you want to delete this customer?"); if (Console.ReadLine().ToLower() == "y") { customers.RemoveAt(deleteId); } return customers; } // ...which would be called from another method with: List<Customer> customers = DeleteCustomer(customers); 

but this does not seem effective, since the same variable is passed by value and then returned.

What is the most efficient way to pass arguments in this case?

+6
source share
2 answers

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

Please note that this is very different from being passed by reference, as this implies that the purpose of this parameter extends to the caller, which does not match

This means that object modifications (such as those performed by RemoveAt ) will be automatically passed to the caller.

So just pass it on; no need for return value or out / ref parameters.

You very rarely use out / ref for reference types, and when using values ​​for types, the difference in performance will be so small compared to returning that you should not worry about it unless you have profiled and made sure that the problem exists. Use what makes the most idiomatic sense.

+4
source

In C #, a parameter is passed by value. This means that when passing a parameter, a copy of the parameter is passed to the method. C # have types by value (e.g. int) and by reference (like any class). C # contains a stack (when all variables are clicked) and a bunch. The value of value types is pushed directly on this stack, and the reference type link is pushed on the stack, and the reference value is inserted into the heap.
When you pass a reference type (e.g. List), it creates a copy of the link, but that copy points to the same object in the list. Therefore, any change directly affects the object, unless you change the link (using assigmet), but this is not your case.

this could be 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 the ref out keyword, I can help you too, but not for this example.

+2
source

Source: https://habr.com/ru/post/979006/


All Articles