c# - C# 中的集合

标签 c#

我来自 C++ 背景,我正在尝试自学 C# 我仍然是一名学生,这不是一个学校项目,我现在正在休息。我的主要关注点是 C/C++、java

我有一个 2 年前用 C++ 完成的旧学校项目,将 DVD、书籍、DVD、信息存储在一个容器中,然后显示它。我正在尝试用 C# 编写相同的程序

我有两个非常相似的问题..

在我的程序中,在容器上使用了 typedef,并像这样创建了容器的对象:

typedef set<Item*>  ItemSet;

ItemSet allBooks;            // holds all the information
ItemSet allCDS;
ItemSet allDVDs;

第一个问题是 C# 中没有 typdef 我认为它的等效项正在使用?但我不能在容器上使用它。有没有办法在 C# 中做类似上面的事情,或者我应该创建多个集合? 我认为 Hashset 类似于 C++ 中的集合

HashSet<Item> keys = new HashSet<Item>();

第二个问题是我还在课外的集合上使用了 typedef,然后在私有(private)部分中使用了 typedef 我做了和对象的集合。我可以在 C# 中执行此操作吗?

typedef set<string> StringSet;

class Item
{

 private:

 string Title;
 StringSet* Keys;      // holds keywords

我确实意识到 C# 没有指针!

最佳答案

C# 没有类似typedef 的东西。您只需直接使用类型名称即可。

HashSet<Item> allBooks = new HashSet<Item>(); 
HashSet<Item> allCDs= new HashSet<Item>(); 
HashSet<Item> allDVDs = new HashSet<Item>(); 
HashSet<string> keys = new HashSet<string>(); 

所以...

class Item
{
    private string title;
    private HashSet<string> keys = new HashSet<string>();      // holds keywords

using 语句声明编译器应搜索以解析类型名称的命名空间。所以为了上面的工作你需要

using System.Collections.Generic; 

在源文件的顶部。

如果没有 using 语句,你必须这样做

System.Collections.Generic.HashSet<Item> allBooks = new System.Collections.Generic.HashSet<Item>(); 

关于c# - C# 中的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8584675/

相关文章:

c# - 如何从收到的 msmq 消息中获取发件人的 WindowsIdentity?

c# - 使用资源文件的多语言支持

c# - 坐在窗口asp.net mvc时添加实体后发生StackOverflowException

c# - 泛型和可空(类与结构)

c# - 不同的构建操作在 csproj 中做了什么。 IE。附加文件或假货

c# - Word Com 互操作 : Iterating all the ranges of a Word document in c#

c# - 在 C# 中从 UTF-8 转换

c# - File.ReadLines 不用加锁吗?

c# - 如何以编程方式调用 C++ 编译器?

c# - SaveChanges(更新、删除)的 EF Core 全局查询过滤器