c# – 在静态类中定义Generic List属性
发布时间:2020-12-15 08:26:32 所属栏目:百科 来源:网络整理
导读:我需要在运行时使用静态类来保存一些简单的值: public static class Globals{ public static string UserName { get; set; } ...more properties} 我现在需要存储一个对象列表 – 根据我定义的类进行输入.此List将重用于不同的Object类型,因此我认为将其定
我需要在运行时使用静态类来保存一些简单的值:
public static class Globals { public static string UserName { get; set; } ...more properties } 我现在需要存储一个对象列表 – 根据我定义的类进行输入.此List将重用于不同的Object类型,因此我认为将其定义为Generic. 需要是这样的: public List<T> Results {get; set;} 解决方法
您需要将类定义为
Generic类.
public static class Globals<T> { public static string UserName { get; set; } public static List<T> Results { get; set; } } 以后你可以使用它: Globals<string>.Results = new List<string>(); 您的房产结果也必须是私人的.您可以考虑公开您的方法与列表进行交互,而不是通过属性公开列表. 见:C#/.NET Fundamentals: Returning an Immutable Collection (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |