list - c# 使用泛型的方法中不能这样赋值吗?

查看:341
本文介绍了list - c# 使用泛型的方法中不能这样赋值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

        public  void Load<T>(List<T> list, string Path)
        {
            try
            {
                using (Stream stream = File.Open(Path, FileMode.Open))
                {
                    BinaryFormatter bin = new BinaryFormatter();
                    List<T> data = (List<T>)bin.Deserialize(stream);
                    list = data;
                }
            }
            catch
            {
                list = new List<T>();
            }
        }
        
        public UserManager(string path)
        {
            Load<User>(UserList, path);
        }

这种做法list总是null,改成下面这样就可以,为什么?..

        public  List<T> Load<T>(string Path)
        {
            try
            {
                using (Stream stream = File.Open(Path, FileMode.Open))
                {
                    BinaryFormatter bin = new BinaryFormatter();
                    List<T> data = (List<T>)bin.Deserialize(stream);
                    return data;
                }
            }
            catch
            {
                return null;
            }
        }
        
        public UserManager(string path)
        {
            if (Load<User>(path) != null)
                UserList = Load<User>(path);
            else
                UserList = new List<User>();
        }

解决方案

https://docs.microsoft.com/zh...

这篇关于list - c# 使用泛型的方法中不能这样赋值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆