I just do it either with a list or with a dictionary. I will show both.
class Example
{
public List<FileInfo> FileList { get; set; }
public Dictionary<string, FileInfo> Files { get; set; }
public Example()
{
FileList = new List<FileInfo>();
Files = new Dictionary<string, FileInfo>();
}
}
You should now use the property as if it were the actual List or Dictionary object.
var obj = new Example();
obj.FileList.Add(new FileInfo("file.txt"));
obj.Files.Add("file.txt", new FileInfo("file.txt"));
obj.Files["file2.txt"] = new FileInfo("file2.txt");
var myListedFile = obj.FileList[0];
var myFile = obj.Files["file.txt"];
I prefer a dictionary approach.
Please note: since the property is publicly available, you can also replace the entire list or dictionary.
obj.Files = new Dictionary<string, FileInfo>();
var otherFiles = new Dictionary<string, FileInfo>();
otherFiles["otherfile.txt"] = new FileInfo("otherfile.txt");
obj.Files = otherFiles;
If you made your own set of properties, you can still call Add (), but not reassign the list or dictionary itself.
source
share