首页 \ 问答 \ C#如何将一个类的实例存储在列表中(C# How to store instances of a class in a list)

C#如何将一个类的实例存储在列表中(C# How to store instances of a class in a list)

我有一个名为User的类。 该类有一个名为Users的静态属性,它是存储所有创建用户的列表。 我希望用户在实例化时自动添加到此列表中,以便将其包含在User类的构造函数块中。

我已经编写了下面的代码,但是当我使用构造函数创建用户时,它们不会添加到“用户”列表中,并且列表仍然为空。

以下是我写的代码。

public class User
{
    private static List<User> _users=new List<User>();
    private string _name;

    public User(string Name)
    {
        _name=Name;
        _users.Add(this);
    }
}

我知道在创建User类的实例后,我可以将每个用户添加到列表中。 但我想知道是否有办法自动执行此操作。

还需要知道在User类中是否有更好的用户列表,或者是创建每个用户的基类。


Removing the question as it's not relevant anymore!


原文:https://stackoverflow.com/questions/40708462
更新时间:2021-12-15 18:12

最满意答案

我想你在这里不需要匿名类型。 尝试这个:

var bestPrice = query.Min(x => CalcInterest(amount, term, x.ProductRate.Rate));

编辑:玩弄它我意识到这创造了不同的结果。 您的代码返回anon类型。 这只返回感兴趣的对象。 不保留兴趣数据。

无论哪种方式,使用Min来获得最小值。


You don't need anonymous types here, I think. Try this:

var bestPrice = query.Min(x => CalcInterest(amount, term, x.ProductRate.Rate));

Edit: playing around with it I realized this created a different result. Your code returns an anon type. This just returns the object which has the lowest interest. The interest data isn't preserved.

Either way, use Min to get the minimum.

相关问答

更多
  • 使用查询语法,您可以引入新的范围变量,它将保存方法参数 from m methods.Where(m => m.ReturnType == typeof(void)) let p = m.GetParameters() where p.Length == 1 && p[0].ParameterType == wantedType select m 方法语法不是那么美丽: methods.Where(m => m.ReturnType == typeof(void)) .Select(m => ...
  • 保持动态: dynamic result = new ExpandoObject(); dt.AsEnumerable() .GroupBy(r => r.Field("type")) .ToList() .ForEach(g=> ((IDictionary)result)["sum" + g.Key.ToUpper()] = g.Sum(r=>r.Field("cnt"))); Console.WriteLine(result.s ...
  • 我想你在这里不需要匿名类型。 尝试这个: var bestPrice = query.Min(x => CalcInterest(amount, term, x.ProductRate.Rate)); 编辑:玩弄它我意识到这创造了不同的结果。 您的代码返回anon类型。 这只返回感兴趣的对象。 不保留兴趣数据。 无论哪种方式,使用Min来获得最小值。 You don't need anonymous types here, I think. Try this: var bestPrice = query. ...
  • 只需将代码的最后一部分更改为: New With {Key l1.id, Key l1.timestamp} 我测试了代码,它的工作原理。 编辑: 我不知道为什么这对你不起作用,我会发布整个代码以确定。 Dim dsNewFiles = From l1 In list1 _ Where Not (From l2 In list2 _ Select l2.ID, l2.TimeS ...
  • NHibernate.Linq NHibernate 2.1.2.4000中有很多东西是行不通的。 您可以使用HQL或ICriteria,或者升级到NHibernate 3.0,或者如果您要使用所有数据,请在Select通过添加ToList强制执行Linq查询。 tags .Select(t = new { t.Name, t.Posts.Count }) .ToList() .OrderBy(x => x.Count); 匿名对象本身就是NHibernate.Linq可以处理的东 ...
  • 根据文档, 匿名类型是引用类型 : 从公共语言运行库的角度来看,匿名类型与任何其他引用类型没有区别。 因此,它将使用 System.Object实现的那些函数的默认实现(至少对于 基于引用相等性的相等性 )。 编辑 :实际上,根据相同的第一个doco链接,它说: 因为匿名类型上的Equals和GetHashCode方法是根据属性的Equals和GetHashcode方法定义的,所以同一匿名类型的两个实例只有在它们的所有属性相等时才相等。 As per the documentation, an anonym ...
  • 要使用Contains,必须在IEnumerable传递该类型的实例。 匿名类型非常困难。 相反,我会使用Any扩展方法重载,它允许您指定比较lambda。 例如 var rows2 = (from t2 in db.TABLE2 where (rows1.Any(x => x.COLUMN_B == t2.COLUMN_A)) select t2; In order to use Contains you must pass an instance of the type in t ...
  • 你的class必须正确覆盖Equals(object)和GetHashCode() 。 否则,即使它们看起来相同,两个new Params { ... }也不会“相等”。 匿名类型会自动覆盖这两个方法。 您也可以使用struct而不是class因为struct使用System.ValueType中存在的Equals(object)和GetHashCode()的覆盖。 如果你选择一个struct ,考虑使类型不可变 ,即使属性为只读(或使用private set; )。 Your class must ove ...
  • 因为您的query变量实际上代表一个查询 ,而不是一组实际的数据。 每次枚举query它都会再次执行操作; 您的声明(分配query变量) 定义查询。 枚举它执行它。 您想要做的是以列表的形式创建查询的内存中表示。 你可以这样做: Dim list = (From X In list Select New With {.Points = 0, X.ID}).ToList() For Each o In list o.Points = 1 Next 这应该会给你你期望的行为。 Because yo ...
  • 在linq查询中,您正在定义一个无法转换为PersonInfo对象的新类型。 要解决此问题,您需要在select中定义PersonInfo对象。 这样你的return语句就和你的对象一样。 List newCustomerList = userList.GroupBy(u=>u.ID) .Select(group=> new PersonInfo() { }).ToList(); 在新的PersonInfo中, ...

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)