views:

150

answers:

2

I know there are no pointers in C#, but I am trying to figure out how to do the following, which I would have done with pointers (or better yet, iterators) in C++ (I am taking a course in C#, but I already know C++).

We got an assignment to write a simple "store" program (inventory, transactions, etc.). My first idea (coming from C++) was this: have a linked list of items and their amount in stock. Then, have a class representing a sale, which has a list of the items in the current sale, where each item is represented as an iterator to specific items in the master stock list and a value for the amount. (I hope this is clear.)

I tried to do the same in C# but can't figure out how to get those iterators to the master list (they should preferably also be good across updates to the master list). How do you do this?

+2  A: 

You don't need to do anything special to use references, they are used by default in C#.

I'm assuming you'd do something like this in C++:

class Sale {
  public:
    void AddItem(Item* i) {
        items.push_back(i);
    }

  private:
    std::vector<Item*> items;
};

In C#, since it uses references rather than pass by value by default, you get that behavior by default. You'd get similar behavior in C# from the following code:

class Sale {
  private List<Item> items;

  public void AddItem(Item i) {
    items.Add(i);
  }
}

To call this C# code you could just do something like

Item item = new Item("A test item");
Sale sale = new Sale();
sale.AddItem(item);
Zareth
This is not even close to what the OP asked for.
DeadMG
@DeadMG The way I read OP, he wants to use pointers so that his "Sale" won't contain copies of items, so any updates done to items in the store master list would also show up in Sales. While he does mention using Iterators, the way I read it he didn't want them for their ability to traverse the list, but rather to serve as a pointer to a specific list item, so once he got one it would only be dereferenced and not incremented/decremented.
Zareth
@Zareth: An iterator's primary purpose is to allow iteration. Hence the name. References won't serve the same purpose, because if you take a reference to a list[0], then change list[0], the reference won't be updated, whereas if you took an iterator to a vector[0], then assigned to vector[0], the change would be updated in the iterator.
DeadMG
@DeadMG I'm sorry if I didn't explain it right, but what Zareth is suggesting is exactly what I was looking for (I think. I haven't actually tried it yet). I hadn't realized that all copies of classes were by definition only references. I thought they were actually copies (I seem to remember that that is true in Java, and I guess I assumed it true for c# too)
baruch
A: 

You would almost certainly have to write your own iterator-style interface - C# doesn't seem to offer anything close to so flexible. You could write your own extension methods and a class that offers this, but you'd have to do it yourself, I'm fairly sure that it doesn't exist in the .NET standard libraries.

DeadMG