How to create a property for a List

前端 未结 6 608
春和景丽
春和景丽 2020-12-13 00:32
private List newList;

public List NewList
{
get{return newList;}
set{newList = value;}
}

I want to create something like this, b

相关标签:
6条回答
  • 2020-12-13 01:04

    You could do this but the T generic parameter needs to be declared at the containing class:

    public class Foo<T>
    {
        public List<T> NewList { get; set; }
    }
    
    0 讨论(0)
  • 2020-12-13 01:10

    Either specify the type of T, or if you want to make it generic, you'll need to make the parent class generic.

    public class MyClass<T>
    {
      etc
    
    0 讨论(0)
  • 2020-12-13 01:16

    T must be defined within the scope in which you are working. Therefore, what you have posted will work if your class is generic on T:

    public class MyClass<T>
    {
        private List<T> newList;
    
        public List<T> NewList
        {
            get{return newList;}
            set{newList = value;}
        }
    }
    

    Otherwise, you have to use a defined type.

    EDIT: Per @lKashef's request, following is how to have a List property:

    private List<int> newList;
    
    public List<int> NewList
    {
        get{return newList;}
        set{newList = value;}
    }
    

    This can go within a non-generic class.

    Edit 2: In response to your second question (in your edit), I would not recommend using a list for this type of data handling (if I am understanding you correctly). I would put the user settings in their own class (or struct, if you wish) and have a property of this type on your original class:

    public class UserSettings
    {
     string FirstName { get; set; }
     string LastName { get; set; }
     // etc.
    }
    
    public class MyClass
    {
     string MyClassProperty1 { get; set; }
     // etc.
    
     UserSettings MySettings { get; set; }
    }
    

    This way, you have named properties that you can reference instead of an arbitrary index in a list. For example, you can reference MySettings.FirstName as opposed to MySettingsList[0].

    Let me know if you have any further questions.

    EDIT 3: For the question in the comments, your property would be like this:

    public class MyClass
    {
        public List<KeyValuePair<string, string>> MySettings { get; set; } 
    }
    

    EDIT 4: Based on the question's edit 2, following is how I would use this:

    public class MyClass
    {
        // note that this type of property declaration is called an "Automatic Property" and
        // it means the same thing as you had written (the private backing variable is used behind the scenes, but you don't see it)
        public List<KeyValuePair<string, string> MySettings { get; set; } 
    }
    
    public class MyConsumingClass
    {
        public void MyMethod
        {
            MyClass myClass = new MyClass();
            myClass.MySettings = new List<KeyValuePair<string, string>>();
            myClass.MySettings.Add(new KeyValuePair<string, string>("SomeKeyValue", "SomeValue"));
    
            // etc.
        }
    }
    

    You mentioned that "the property still won't appear in the object's instance," and I am not sure what you mean. Does this property not appear in IntelliSense? Are you sure that you have created an instance of MyClass (like myClass.MySettings above), or are you trying to access it like a static property (like MyClass.MySettings)?

    0 讨论(0)
  • 2020-12-13 01:16
    public class MyClass<T>
    {
      private List<T> list;
    
      public List<T> MyList { get { return list; } set { list = value; } }
    }
    

    Then you can do something like

    MyClass<int> instance1 = new MyClass<int>();
    List<int> integers = instance1.MyList;
    
    MyClass<Person> instance2 = new MyClass<Person>();
    IEnumerable<Person> persons = instance2.MyList;
    
    0 讨论(0)
  • 2020-12-13 01:23

    It's possible to have a property of type List<T> but your class needs to be passed the T too.

    public class ClassName<T>
    {
      public List<T> MyProperty { get; set; }
    }
    
    0 讨论(0)
  • 2020-12-13 01:29

    Simple and effective alternative:

    public class ClassName
    {
        public List<dynamic> MyProperty { get; set; }
    }
    

    or

    public class ClassName
    {
        public List<object> MyProperty { get; set; }
    }
    

    For differences see this post: List<Object> vs List<dynamic>

    0 讨论(0)
提交回复
热议问题