user249375
user249375

Reputation:

collections in C#

I am coming from a C++ background and i am trying to teach myself C# I am still a student and this isnt a school project and that i am on break right now. My main focus has been C/C++, java

I have an old school project from 2 years back that i did in C++ that stores DVD, book, DVD, info in a container and then displaying it. I am trying to make the same program in C#

I have 2 questions that are very similar..

In my program a used typedef on the container and made objects of the container like so:

typedef set<Item*>  ItemSet;

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

First question is there is no typdef in C# i think its equivalent is using? But i cant use it on a container. Is there a way to do something similar above in C# or should i just create multiple sets? I decided that Hashset is similar to set in C++

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

Second question is i also used typedef on a set outside the class and then in the private section i made and object of the set. Can i do this in C#?

typedef set<string> StringSet;

class Item
{

 private:

 string Title;
 StringSet* Keys;      // holds keywords

I do realize C# has no pointers!

Upvotes: 4

Views: 371

Answers (4)

Tim S.
Tim S.

Reputation: 56536

There is an equivalent to typedef with the using directive, but I haven't seen it commonly used. The full types are usually used. If you have the need, (e.g. you need to store other data alongside the collection of items) create a class like public class ItemSet : List<Item>.

HashSet<T> should be used when the set should not contain the same item two or more times, and the Object.Equals and Object.GetHashCode methods are meaningful for your data type (by default, they essentially identify the instance, not the value of an object). It will automatically use these to only add an object if it does not already exist in the set. A far more common approach is to use a List<T> to store items. From what little I know of your situation, a List<T> should work.

So I'd recommend something like:

class Item
{
    private string Title;
    List<string> Keys;      // holds keywords
}

And:

List<Item> allBooks = new List<Item>(); 
List<Item> allCDs= new List<Item>(); 
List<Item> allDVDs = new List<Item>(); 

Upvotes: 1

Andrew Cooper
Andrew Cooper

Reputation: 32576

C# doesn't have anything like typedef. You just use the type names directly.

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

So...

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

The using statement declares the namespaces that the compiler should search to resolve the type names. So for the above to work you need

using System.Collections.Generic; 

at the top of the source file.

Without the using statement you'd have to do

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

Upvotes: 3

vcsjones
vcsjones

Reputation: 141638

C# doesn't explicitly have a typedef, the closest thing is a using declaration to create a type alias. At the top of your C# file you could do something like:

using StringSet = System.Collections.Generic.HashSet<string>;

Then later on you could do something like:

var set = new StringSet();

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

There is no concept that parallels C++'s typedef in C#. To make your life easier and save you some typing, C# lets you skip the type in declaration/initialization of local variables:

var keys = new HashSet<Item>();

is the same as

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

You still need to use the full type in member declarations. However, it is a good practice to declare members as interfaces, not as exact types:

class MyClass {
    ISet<Item> allBooks = new HashSet<Item>();
}

This could let you switch implementation at a later time without having to change anything else in your code.

Upvotes: 2

Related Questions