So, you say, that I don't need ref in method
We hardly ever need ref, anywhere, ever. My advice to new programmers is to avoid it, avoid `out`, and avoid `static`
If you want to return multiple things from a method, use a Tuple or make a class so you can return eg a Book that represents multiple related bits of information like Author and Title. If you're trying to use ref because you want some method to swap out an object for another, even when it's none of that method's business to do so, then don't; it leads to obscure bugs and hard to follow logic if e.g. the person that borrowed a book is capable of swapping it for a new one and affecting the library contents
Avoid `static` because static is like a weird opposite universe where there is only ever one of anything, and that's more or less the opposite of what you're trying to achieve with OOP. The OO universe can reach into the static universe but not the other way round, which means that a beginner's (usually) poorly structured programs start acquiring `static` all over the place because the developer "just want the 'an object reference is required for the non static..' error messages to go away"
we must get link, but when we change object
It's not the responsibility of the person borrowing the book, to change it to something else. The book never leaves the library's management system. You look the book record up, you change the property that tracks who has borrowed it, and maybe the date, and there it stays in the list, but updated with new properties (or if you're on a non mutable streak you make a new object with all the old properties and the new borrower/date info.
What you're currently trying to do is conceptually give the person borrowing the book the keys to the library and the password to the computer, so that they have the power to sneak in late at night and change out the book for another one, and erase the knowledge the library computer has that there ever was the old book in the library
I tried create a method that will be change selected book to emty.
And it looks fine. If it didn't work, check that the book title you entered really was found; c# is case sens in your code
Does it mean that when we get object from list, we get copy of it?
If it's a class, then no; your variable reference points to the original data. This is a point of confusion for many newbies. The original data that makes up a class lives in one place in memory and it is not copied unless you explicitly perform a cloning operation (which you have to implement explicitly)
var book = new Book("Bob C Martin", "Clean Code");
var book2 = book;
var books = new List<Book>();
books.Add(book);
var book3 = books[0];
books.Add(book3);
In all these lines, there is still only one set of book data, and there are 5 different references to it. If it were a dog, it has 5 leads attached to its collar.
Every time we do `var x = ...` we're making another variable reference and then with the assignment, we're making it points to the same data. You have 5 copies of a variable, but only one set of data.
They do not chain like this:
Everything individually points to the original data. If you change what `book` points to, all the others stay pointing to the original data
book = new Book("Another", "Book");
Gives:
You really don't need to. In the case where you have multiple references to one book, you can alter something about the book and all references see it
book.Author = "JKR";
book.Title = "HPotter";
But you cannot(should not) try to arrange a setup where any one variable reference is capable of swapping out the entire book object for a whole new one. That's realistically the job of just one of your objects- the library. If the librarian decides to throw out some old book and store a new one on position 2 in the shelf then the method that does that should be inside the library. It should not be something the person who borrowed the book is capable of doing
If you're doing this because you're trying to make some sort of internal find method inside library, that can be used in various places, consider returning the index the book was found at instead of returning the book:
class Library{
List<Book> books = new();
private int FindIndex(string t){
for(int x = 0; x<books.Count; x++){
if(books[x].Title.Contains(t) return x;
}
return -1;
}
public Book GetBookByTitle(string title){
var idx = FindIndex(title);
return books[idx];
}
public void EraseBookByTitle(string title){
var idx = FindIndex(title);
books[idx] = new Book();
}