using and return (what do they do under the sheets . . .)

  • Thread starter Thread starter Robert May
  • Start date Start date
R

Robert May

Question,

Say I have the following:

Using(DataSet ds=new DataSet)
{
...
da.fill(ds)
...
return ds;
}

What happens? Does the using dispose the dataset? Return is supposed to
stop execution immediately, correct?

Just wondering.

Robert
 
rakkerspamisevil91 said:
Question,

Say I have the following:

Using(DataSet ds=new DataSet)
{
...
da.fill(ds)
...
return ds;
}

What happens? Does the using dispose the dataset?
Yes.

Return is supposed to
stop execution immediately, correct?

No, return is supposed to return a value from the function... :)

But yes, return "exits" the function, but when .NET handles the "exit"
it see the "try/finally" block that is generated with a "using"
statement and executes the finally block before returning the value
(which disposes of the dataset).
 
Robert May said:
Question,

Say I have the following:

Using(DataSet ds=new DataSet)
{
...
da.fill(ds)
...
return ds;
}

What happens? Does the using dispose the dataset? Yes.
Return is supposed to
stop execution immediately, correct?

Except for Finally blocks which are guaranteed to run. The using block is
just shorthand for

try
{
da.fill(ds)
...
return ds;
}
finally
{
ds.Dispose();
}


Here after the return statement the finally block is executed.

You shouldn't be Disposing your DataSets. They don't really need it.
DataSet inherits IDisposable from MarshalByValueComponent. This is all junk
required for dragging and dropping DataSets onto Forms in design view, and
just confuses the runtime behavior of the the DataSet. Disposing a DataSet
doesn't really do anyting, so in your code your function will return a
disposed, but perfectly useful DataSet.

David
 
Back
Top