c# - 将静态值分配给另一个类的类

标签 c#

我的问题非常基本,但我没有想法去做。请检查下面的代码。我的基本目标是制作一个公共(public)类,它将在列表下返回一些静态数据。下面提供了示例类模型。在类 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; }
    }

}

最佳答案

试试这个,希望这有帮助

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; }
        }

    }

关于c# - 将静态值分配给另一个类的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53789328/

相关文章:

c# - ASP.NET MVC 自定义 View 路由

c# - 我可以将属性与匿名类一起使用吗?

c# - 为什么 awating 后的代码在不同的线程上运行(即使设置了 SynchronizationContext)?

c# - LINQ to SQL 查找未在另一个列表中列出的对象

c# - 动态选择数据库

c# - C# 中的 MemoryStream 导致 Xml 模式验证失败

c# - word文档(.doc/.docx)在用户上传后如何统计字数?

c# - Unity - 如何对同一类型使用多个映射并注入(inject)到一个对象中

c# - Windows窗体中多对多关系的数据输入

c# - 使用 ASP.NET MasterPage 时按正确顺序加载表