-1

I have this nested list :

 public List<List<string>> pcList = new List<List<string>>();

How can I create getters and setters for it? I have tried everything out there on the internet and nothing seems to work

Thx!

EDIT: So, In class Pc I have this code:

 class PC
{
    public List<List<string>> pcList = new List<List<string>>();
    public List<string> subList = new List<string>();

  }

so the pcList is the "parent" list and the sublist is actually the place where I add each pc with its info. I have a method where I populate the lists. Then I want to use an object of this class in another class called X, let's say. I have tried to simply access the lists by using object.ListName but it doesn't work.

alexxa
  • 39
  • 1
  • 9
  • Still very unclear what you have problem with - can you show code that you want to get working? Also what is the point of adding `subList` to the sample - how it is related to `pcList`? – Alexei Levenkov Apr 06 '20 at 19:10

3 Answers3

1

You can use auto property - just add {get;set;} (note that usually properties are upper case)

 public List<List<string>> PcList {get;set;}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • If I do this , I get System.NullReferenceException: – alexxa Apr 06 '20 at 18:59
  • @alexxa but you've hopefully read https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it and now how to deal with NRE already... Clarifying exactly what you have problem with along with your requirements is important to get answer to question *you have* and not one *you wrote*... Consider editing your question to clarify what you are looking for. – Alexei Levenkov Apr 06 '20 at 19:04
  • @alexxa `public List> PcList {get;set;}` could be `public List> PcList {get;set;} = new List>();`. This would instantiate the `PcList`. – Trevor Apr 06 '20 at 19:30
1
public List<List<string>> pcList { get; set; } = new List<List<string>>();
rfmodulator
  • 3,638
  • 3
  • 18
  • 22
0

This is a variable. If you want a property declare it like:

public List<List<string>> pcList {get;set;}