为另一个班级的班级分配静态值

时间:2018-12-15 03:41:40

标签: c#

我的问题几乎是基本的,但我不知道要这样做。请检查下面的代码。我的基本目标是创建一个公共类,该类将在列表下返回一些静态数据。下面提供了示例类模型。请参见类PaymentMethodDetials中的两个属性,我想将类PaymentMethodList中这两个属性的值设置为列表,然后我将在整个c#类模型之外公开使用这些列表值。现在我的问题是paymentList.Add() Visual Studio不允许我执行Add方法。我该如何解决?预先感谢

namespace Test.Helpers
{
    public class PaymentMethodList
    {

        List<PaymentMethodDetials> paymentList = new List<PaymentMethodDetials>();
        paymentList.Add()//i want to insert data to "PaymentMethodDetials" this class like using "Add" which allowing now
    }

    public class PaymentMethodDetials
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

}

1 个答案:

答案 0 :(得分:4)

尝试一下,希望对您有帮助

namespace Test.Helpers
    {
        public class PaymentMethodList
        {

            List<PaymentMethodDetials> paymentList = new List<PaymentMethodDetial();
            public PaymentMethodList()
            {
               paymentList.Add(new PaymentMethodDetials
              {
                Id=1,
                Name="xyz"
              });
            }
           
        }

        public class PaymentMethodDetials
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

    }