Store values to collection, suggestion please
Novice question. What collection type should I use to store such structure?
Files collection
---------------
FileId: Int
FileName : String
Path: String
I will need to store several files data into that collection, find Path by FileId field, delete i开发者_如何转开发tem by FileId field.
I would create a Dictionary
with a custom class as value:
public class FileInfo
{
public string FileName {get;set;}
public string Path {get;set;}
}
and store in a Dictionary<int, FileInfo>
dictionary where you keep FileId
as key in the Dictionary
.
Since you have several pre-defined pieces of information that you want associated with each value instance, I would start by defining a structure to hold the information required for each file:
public struct CustomFileInfo //pick a name that avoids conflicts with System.IO
{
public readonly int FileId;
public readonly string FileName;
public readonly string Path;
// constructor
public CustomFileInfo(int fileId, string fileName, string path)
{
this.FileId = fileId;
this.FileName = fileName;
this.Path = path;
}
}
And then I would use a generic collection (such as a List<T>
) to hold instances of that structure:
List<FileInfo> myFiles = new List<FileInfo>();
Alternatively, you could use a Dictionary<TKey, TValue>
, with the FileId
as the key and the FileInfo
structure as the value. For example:
Dictionary<int, FileInfo> myFiles = new Dictionary<int, FileInfo>();
The advantage of a dictionary is that it provides faster lookup time when searching for a file by its ID. This is because a dictionary is implemented internally using a hash table.
EDIT: Note that mutable structs are evil. If you need to be able to change the individual pieces of information that describe a file (and I can't imagine why you would), you should declare CustomFileInfo
as a class
instead of a struct
.
There are several approaches possible.
Assuming a structure like this:
class File
{
int FileId { get; set; }
string FileName { get; set; }
string Path { get; set; }
}
Simple generic list
You can hold your data in a simple generic list:
List<File> File { get; set; }
Observable collection
If you need to bind the collection to a WPF UI and its content is update from somewhere in your code, I suggest:
ObservableCollection<File> Files { get; set; }
Dictionary
If FileId is unique and it is the only property you use to find the items, you can put them into a dictionary like this:
Dictionary<int, File> File { get; set; }
Create a File
class:
public class File
{
public int Id { get; private set; }
public string Path { get; private set; }
public string FileName
{
get
{
return System.IO.Path.GetFileName(this.Path);
}
}
public File(int id, string path)
{
this.Id = id;
this.Path = path;
}
}
Then, you can either store the objects in a List<File>
:
List<File> files = GetFiles();
files.RemoveAll(f => f.Id == 42);
..or in a Dictionary<int, File>
:
Dictionary<int, File> files = GetFiles(); // GetFiles should use File.Id as key
files.Remove(42);
The upside with the Dictionary
is that you have extremely efficient lookup on the key. The upside with the List
is that you have flexibility and can easily write code to remove based on any value of the contained File
objects, such as:
files.RemoveAll(f => f.Path.Contains("stuff"));
精彩评论