JOE SKEET
JOE SKEET

Reputation: 8108

why does datatable.Clear, clears the other datatable as well?

DataTable dttemp = new DataTable();
dttemp=orderedTable;
dttemp.Rows.Clear();

for some reason the orderedTable is being cleared as well! does anyone know why this is happening?

Upvotes: 2

Views: 1157

Answers (7)

Yochai Timmer
Yochai Timmer

Reputation: 49251

In C# class variables are saved by reference, which means that the variable itself is something like a pointer to the real data. So the variables (except for primitives) don't really hold the data, but just point to the data you assign to it.

In your case, you point to an empty table :

DataTable dttemp = new DataTable();

but then you make it point to another table:

dttemp=orderedTable;

This effectively causes two things:

  1. The variable dttemp now points to the orderedTable

  2. The new table you created in the first line has nothing pointing at it. It's lost in the memory with no reference to it, and will be finalized when the Garbage Collector runs.

Upvotes: 0

marc_s
marc_s

Reputation: 754628

Because your dttemp is now referencing orderedTable - they both point to the same location in memory.

This line of code:

dttemp = orderedTable;

does not do a full, deep copy! It just points dttemp to the same place that orderedTable lives at....

So if you clear the .Rows(), then those are gone regardless of whether you're lookign at it through dttemp or orderedTable.

If you want a real, full, deep copy, use this:

dttemp = orderedTable.Copy();   // copies structure and data

or

dttemp = orderedTable.Clone();  // copies the structure, but no data

Upvotes: 3

gsharp
gsharp

Reputation: 27927

because it's a reference type and not a value type.

Upvotes: 0

Aaron McIver
Aaron McIver

Reputation: 24713

dttemp=orderedTable;

That line assigns orderedTable to dttemp. Making calls on dttemp will then by definition affect orderedTable.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1501033

By the time you've executed this line:

dttemp = orderedTable;

there's only actually one DataTable which is relevant. The DataTable you created in the first line of code is eligible for garbage collection - nothing's taking any notice of it. The line quoted here doesn't make a copy of the object - it makes a copy of the reference, so that dttemp and orderedTable refer to the same DataTable.

DataTable is a reference type. It's absolutely vital that you understand how reference types work in C# - otherwise the whole language will have baffling results for you. I have an article which you may find useful along those lines.

To put your code into context, imagine that a DataTable is actually a house. You give your home address to two different people. One goes and paints the house red... and then the other visits the house. Obviously he'll find it's red.

The important thing is that you didn't give each person "your house" - you just gave them a way of getting to your house. That's how reference types work in C#.

The value of dttemp isn't a DataTable - it's a reference to a DataTable. Changes made to that object via any other variables will still be seen via dttemp.

Upvotes: 8

Chandu
Chandu

Reputation: 82913

orderedTable is just holding a reference to the dttemp object hence when you change dttemp you are changing the data in memory which is the same object as referred by orderedTable.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1063013

You have two variables (dttemp and orderedTable) which, at the time when you call Clear(), point at the same object. Thus a change to the object pointed to by dttemp is also a change to the object pointed to by orderedTable.

The table you create with new DataTable() is simply discarded onto the heap (waiting to be garbage-collected) when you assign dttemp=orderedTable;

Upvotes: 4

Related Questions