A class property as a collection

Hi,

I need to include a property in my class, which is a collection of System.IO.FileInfo objects. I'm not quite sure how to do this and how to add and remove objects from an instance of the class (I would assume that any other collection).

Please let me know if I need to add additional information.

thank

Update: Am I approaching this incorrectly? I read the comments that adding to a collection, which is a property, is bad practice. If true, what is good practice? I have a bunch of objects that I need to store in a collection. The collection will be added and deleted before the final action is taken. Is this the right approach or am I missing something?

+3
source share
4 answers

File- static class. Therefore, suppose you had in mind FileInfo.

There are many ways:

  • Open private field
  • Using Iterators
  • Open private field via ReadOnlyCollection <>

For instance,

class Foo {
    public IEnumerable<FileInfo> LotsOfFile {
        get {
            for (int i=0; i < 100; i++) {
                yield return new FileInfo("C:\\" + i + ".txt");
            }
        }
    }
    private List<FileInfo> files = new List<FileInfo>();
    public List<FileInfo> MoreFiles {
        get {
            return files;
        }
    }
    public ReadOnlyCollection<FileInfo> MoreFilesReadOnly {
        get {
            return files.AsReadOnly();
        }
    }

}

With this code, you can easily add to the property MoreFiles:

Foo f = new Foo();
f.MoreFiles.Add(new FileInfo("foo.txt"));
f.MoreFiles.Add(new FileInfo("BAR.txt"));
f.MoreFiles.Add(new FileInfo("baz.txt"));
Console.WriteLine(f.MoreFiles.Count);
+1
source
using System.Collections.ObjectModel;

public class Foo
 { private Collection<FileInfo> files = new Collection<FileInfo>();
   public Collection<FileInfo> Files { get { return files;} }
 }

//...
Foo f = new Foo();
f.Files.Add(file);
+1
source

- ( VB.Net)

Public ReadOnly Property Files As Generic.List(Of IO.File)
    GET
        Return _Files
    END GET
END Property

_Files - Generic.List(Of IO.File), . , List. , , , , , , .

0

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")); // List<>
obj.Files.Add("file.txt", new FileInfo("file.txt")); // Dictionary<>
// also
obj.Files["file2.txt"] = new FileInfo("file2.txt"); // Dictionary<>

// fetch 
var myListedFile = obj.FileList[0]; // List<>
var myFile = obj.Files["file.txt"]; // Dictionary<>

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>();
// or
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.

0
source

Source: https://habr.com/ru/post/1697894/


All Articles