类实例的C#字典 [英] C# dictionary of class instances
问题描述
你好
我正在创建一个音乐播放器.
我有这个课:
Hello
I am creating a music player.
I have this class:
class Song
{
internal int length { get; set; } //the length of the song
internal string FullPath { get; private set; } //full path like C:\\Music\song.mp3
internal string Directory { get; private set; } //e.g C:\\Music
internal string Name { get; private set; } // e.g song.mp3
public Song(string path)
{
FullPath = path;
Directory = System.IO.Path.GetDirectoryName(path);
Name = System.IO.Path.GetFileNameWithoutExtension(path);
}
}
我还有一个字典,其中包含用户输入的每首歌曲的此类的所有实例.
我正在寻找一种检索
I also have a dictionary which holds all the instances of this class for each song which the user inputs.
I am looking for a way to retrieve the
int length
只需使用歌曲名...就可以从字典中存储的"Song"类的任何实例中获取歌曲.
我检索歌曲名称的方法是获取我正在使用的Windows Media Player控件的URL.
所以我正在寻找这样的东西
from any instance of the class ''Song'' stored in the dictionary just by using the song name...
The way I retrieve the song name is by getting the URL of the Windows Media Player control I''m using..
so I am looking for something like this
int x = Dictionary[wmp.URL.ToString()].length
(我知道代码将无法工作,但我只是想对自己的要求有所了解)
我真的希望有人能够理解我的要求并提供帮助:)
(I know that code will not work but I just wanted to give an idea of what I was asking)
I really hope there is someone who can understand what I''m asking and can help :)
推荐答案
首先,System.Collections.Generic.Dictionary<TKey, TValue>
具有两个通用参数,而不是一.访问是通过键完成的,因此您需要键的类型和值的类型.密钥应该是唯一的.它是什么?说,这首歌的完整标题,然后是一个字符串.如果不是,则可能是标题,首次发行年份和作者姓名的组合.在这种情况下,您需要一个键的类或结构.另外,您还需要使用键GetHashCode
覆盖键类型中的等价/身份方法.如果您覆盖System.Object.Equals
,则实际上正式需要哈希码覆盖,并且该键实际上由键/值关联的容器使用.
请参阅:
http://msdn.microsoft.com/en-us/library/system.object.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx [ ^ ].
一个较小的问题是多余的Directory
和Name
(您的解释中的文件名).这只是一个设计错误(请参阅: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself [ ^ ]).这样,可以创建相互矛盾的实例.永远不要做这样的事情.说,引入全路径FileName
;但是您还可以具有文件名和目录的属性,但是可以基于全名和使用System.IO.Path
方法即时返回值.
—SA
First of all,System.Collections.Generic.Dictionary<TKey, TValue>
has two generic parameters, not one. The access is done via the keys, so you need a type for a key and a type for a value. The key should be unique. What is it? Say, the full title of the song, then a string. If not, it could be a combination of the title, the year of first release and the names of the authors. In this case, you need a class or a structure of the keys. Also, you need to override the equivalence/identity methods in the key type with itsGetHashCode
; the hash code override is actually formally required if you overrideSystem.Object.Equals
and is actually used by key/value associated containers.
Please see:
http://msdn.microsoft.com/en-us/library/system.object.aspx[^],
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^].
One minor problem is redundantDirectory
andName
(file name in your interpretation). This is just a design bug (please see: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^]). In this way, it''s possible to create instances contradicting to each other. Never do such things. Say, introduce a full-pathFileName
; but you can also have properties for file name and directory, but returning values on the fly based on full name and the use ofSystem.IO.Path
methods.
—SA
这篇关于类实例的C#字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!