Faced such a problem. There is a class User . It has a SaveUser method that writes only 1 entry to the file. There is a LoadUsers method that returns a list of all users written to the file. An error occurs in the line with the comment. It cannot convert User to User []. How to solve this problem without rewriting the SaveUser method?
class User { private string name; private string password; private string key; private static string fileName = @"Data\users.dat"; public void SaveUser() { BinaryFormatter formatter = new BinaryFormatter(); // получаем поток, куда будем записывать сериализованный объект using (FileStream fs = new FileStream(fileName, FileMode.Append)) { formatter.Serialize(fs, this); } } public static List<User> LoadUsers() { List<User> listUser = new List<User>(); BinaryFormatter formatter = new BinaryFormatter(); //десериализация из файла people.dat using (FileStream fs = new FileStream(fileName, FileMode.Open)) { if (fs == null) return listUser; else { User[] deserilizeUsers = (User[])formatter.Deserialize(fs);// ошибка, нельзя преобразовать User в User[] foreach (User user in deserilizeUsers) { listUser.Add(user); } } } return listUser; } }