Ste*_*oka 5 c# intellisense dictionary
有没有办法添加注释来记录 Dictionary 或 ConcurrentDictionary 以了解键/值的含义?
例如:
Dictionary<guid, string> _users;
Run Code Online (Sandbox Code Playgroud)
This example has a dictionary of users. The guid is the UserId and the string is the username, but it's hard to tell other than just "knowing".
Is there a way to add docs so that when you add items, intellisense tells the developer a note about the key & value?
I know I can add a <summary> comment above it and it puts that note in the object itself, but was looking for when adding, removing, etc.
最近在 GOOS 书中我发现了将常见类型(例如集合)包装在自己的类中的有趣想法:
尝试使用您正在处理的问题的语言,而不是 .Net 构造的语言。它减少了领域和代码之间的概念差距。还尝试限制使用泛型传递类型。这是一种重复形式。这暗示存在应该提取到类型中的领域概念。
坦率地说,我在包装常见的泛型集合方面并没有那么极端,但即使给出类型自己的名称也可以使其匹配更容易阅读和理解:
public class UserNameDictionary : Dictionary<int, string>
{
}
Run Code Online (Sandbox Code Playgroud)
很简单。现在最好读什么:
Dictionary<int, string> users = new Dictionary<int, string>();
UserNameDictionary users = new UserNameDictionary();
Run Code Online (Sandbox Code Playgroud)
您还可以快速向您的班级添加评论:
/// <summary>
/// Represents a dictionary of user names accessed by ids.
/// </summary>
Run Code Online (Sandbox Code Playgroud)
这不会给像Add(int, string)这样的方法添加注释,但是当其他人使用这个类时,他们会在 的上下文中思考UserNameDictionary,而不是在抽象Dictionary<int, string>上下文中。
如果你想让你的类更方便,你可以隐藏基类方法:
public new void Add(int userId, string userName)
{
base.Add(userId, userName);
}
Run Code Online (Sandbox Code Playgroud)
对于更复杂的用例,我会使用将工作委托给内部字典的自定义类。