Ankush Jain
Ankush Jain

Reputation: 1527

little bit confusion in object reference in c#

I have a simple question but i am confused so asking. What is difference between these two scenarios.

1)

DataSet ds = getUsers();

2)

DataSet ds = new DataSet();
ds = getUsers();

Upvotes: 2

Views: 101

Answers (2)

System Down
System Down

Reputation: 6260

In version 1 here's what happens:

  1. getUsers creates a new DataSet object.
  2. The reference gets stored in ds.

In version 2 here's what happens:

  1. a new DataSet object is instantiated and the reference to it is stored in ds.
  2. getUsers creates a different DataSet object.
  3. The reference to this new DataSet gets stored in ds.
  4. The old DataSet now no longer has any variables referencing it and will be picked up by the garbage collector.

Upvotes: 4

David Arno
David Arno

Reputation: 43254

Version (2) creates a new DataSet then hands it over to be garbage collected. Aside from that, they are the same.

Upvotes: 4

Related Questions