tags:

views:

108

answers:

3

I have a strongly-typed datatable and I search for a row by primary key (FyndBy) and them if the row exists I want to delete it. From a style perspective which of the methods below do you prefer?

MyDataRowType selectedRow = table.FindByTablePrimaryKey(something);
if (selectedRow != null)
    selectedRow.Delete();

or

if (table.FindByTablePrimaryKey(something) != null)
    table.FindByTablePrimaryKey(something).Delete();
+7  A: 

Absolutely the first. Using the second will require the table to be searched twice and it is also harder to read. (IMHO)

Rune Grimstad
+1  A: 

Generally id say the first example..

Moulde
+1  A: 

The technical reason for choosing the first one is that you're using a simple pointer (usually just 4 bytes of memory) to store a reference to the row - that is, by using just 4 bytes you gain in not scanning the table again, which takes up a lot of resources (depending on table size, of course).

Seb