我需要在C#中的新初始化列表? [英] Do I need to initialize a list with new in c#?

查看:171
本文介绍了我需要在C#中的新初始化列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

 公共类CityDetailViewModel 
{
公开的IEnumerable<城市。网格和GT;详细{搞定;组; }
公众的SelectList以下状态{搞定;组; }
公共字符串主题{搞定;组; }
公众的SelectList类型{搞定;组; }
}

在我的代码有:

 公众的ActionResult getGrid(字符串PK,串RK){
变种MS =新的List<长>();
变种SW = Stopwatch.StartNew();
变种VM =新CityDetailViewModel();



我想把变量毫秒到我CityDetailViewModel类。

 公共类CityDetailViewModel 
{
公开的IEnumerable< City.Grid>详细{搞定;组; }
公众的SelectList以下状态{搞定;组; }
公共字符串主题{搞定;组; }
公众的SelectList类型{搞定;组; }
公开名单<长> MS {搞定;组; }
}

这是做的正确方法。我不知道,但我需要用新。此外,在我的代码添加到列表中使用:

  ms.Add(sw.ElapsedMilliseconds); 

如果它是CityDetailViewModel的一部分,我还可以做,使用:

  MS.Add(sw.ElapsedMilliseconds); 


解决方案

当您创建的新实例 CityDetailViewModel ,会员 MS ,因此
通话添加它会提高一个的NullReferenceException



您可以创建一个新的列表<长> 里面的类的构造函数,或者创建它的一个新的外

 公共类CityDetailViewModel 
{
...

公共CityDetailViewModel()
{
this.MS =新的List<长>();
}
}

公众的ActionResult getGrid(字符串PK,串RK){
变种SW = Stopwatch.StartNew();
变种VM =新CityDetailViewModel();
...
vm.MS.Add(sw.ElapsedMilliseconds);

 公众的ActionResult getGrid(字符串PK,串RK){
变种MS =新的List<长>();
变种SW = Stopwatch.StartNew();
变种VM =新CityDetailViewModel(){MS =毫秒};
...
ms.Add(sw.ElapsedMilliseconds);



因为 MS vm.MS 将在这里同一个列表实例。


I have the following class:

public class CityDetailViewModel
{
    public IEnumerable<City.Grid> Detail { get; set; }
    public SelectList Statuses { get; set; }
    public string Topics { get; set; }
    public SelectList Types { get; set; }
}

In my code I have:

    public ActionResult getGrid(string pk, string rk) {
        var ms = new List<long>();
        var sw = Stopwatch.StartNew();
        var vm = new CityDetailViewModel();

I want to put the variable ms into my CityDetailViewModel class.

public class CityDetailViewModel
{
    public IEnumerable<City.Grid> Detail { get; set; }
    public SelectList Statuses { get; set; }
    public string Topics { get; set; }
    public SelectList Types { get; set; }
    public List<long> MS { get; set; }
}

Is this the correct way to do it. I'm not sure but do I need to use "new". Also in my code I add to the list using:

ms.Add(sw.ElapsedMilliseconds);

If it is part of the CityDetailViewModel can I still do that using:

MS.Add(sw.ElapsedMilliseconds);

解决方案

When you create a new instance of CityDetailViewModel, the member MS will be null, hence calling Add on it will raise a NullReferenceException.

You can either create a new List<long> inside the class' constructor, or create a new one outside of it

public class CityDetailViewModel
{
     ...

     public CityDetailViewModel()
     {
         this.MS = new List<long>();
     }
}

public ActionResult getGrid(string pk, string rk) {
    var sw = Stopwatch.StartNew();
    var vm = new CityDetailViewModel();
    ...
    vm.MS.Add(sw.ElapsedMilliseconds);

or

public ActionResult getGrid(string pk, string rk) {
    var ms = new List<long>();
    var sw = Stopwatch.StartNew();
    var vm = new CityDetailViewModel() { MS = ms };
    ...
    ms.Add(sw.ElapsedMilliseconds);

since ms and vm.MS will be the same list instance here.

这篇关于我需要在C#中的新初始化列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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