Skip to content

Commit 16b4743

Browse files
committed
Use null-conditional operator (?.) for disposables
Previously, some of the `finally` blocks still used the long form conditions (e.g., `if (command != null) command.Dispose()`). This updates any remaining syntax to instead use the null-conditional operator (e.g., `command?.Dispose()`) for both consistency and succinctness.
1 parent ed9ad43 commit 16b4743

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

OnTopic.Data.Sql/SqlTopicRepository.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
\=============================================================================================================================*/
66
using System;
77
using System.Data;
8-
using System.Diagnostics;
98
using System.Diagnostics.CodeAnalysis;
109
using System.Globalization;
1110
using System.Linq;
@@ -389,9 +388,9 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
389388
| Close connection
390389
\-----------------------------------------------------------------------------------------------------------------------*/
391390
finally {
392-
if (command != null) command.Dispose();
393-
if (connection != null) connection.Dispose();
394-
if (attributes != null) attributes.Dispose();
391+
command?.Dispose();
392+
connection?.Dispose();
393+
attributes.Dispose();
395394
}
396395

397396
/*------------------------------------------------------------------------------------------------------------------------
@@ -473,8 +472,8 @@ public override void Move(Topic topic, Topic target, Topic? sibling) {
473472
| Close connection
474473
\-----------------------------------------------------------------------------------------------------------------------*/
475474
finally {
476-
if (command != null) command.Dispose();
477-
if (connection != null) connection.Dispose();
475+
command?.Dispose();
476+
connection.Dispose();
478477
}
479478

480479
/*------------------------------------------------------------------------------------------------------------------------
@@ -532,8 +531,8 @@ public override void Delete(Topic topic, bool isRecursive = false) {
532531
| Close connection
533532
\-----------------------------------------------------------------------------------------------------------------------*/
534533
finally {
535-
if (command != null) command.Dispose();
536-
if (connection != null) connection.Dispose();
534+
command?.Dispose();
535+
connection?.Dispose();
537536
}
538537

539538
}
@@ -615,8 +614,8 @@ private static string PersistRelations(Topic topic, SqlConnection connection, bo
615614
| Close connection
616615
\-----------------------------------------------------------------------------------------------------------------------*/
617616
finally {
618-
if (command != null) command.Dispose();
619-
if (targetIds != null) targetIds.Dispose();
617+
command?.Dispose();
618+
targetIds.Dispose();
620619
//Since the SQL connection is being passed in, do not close connection; this allows command pooling.
621620
//if (connection != null) connection.Dispose();
622621
}

0 commit comments

Comments
 (0)