Click here to Skip to main content
15,891,905 members
Home / Discussions / C#
   

C#

 
AnswerRe: adding double quote around variable add one backslash which causing issue Pin
Richard Deeming31-Jul-19 7:46
mveRichard Deeming31-Jul-19 7:46 
GeneralRe: adding double quote around variable add one backslash which causing issue Pin
Mou_kol31-Jul-19 21:30
Mou_kol31-Jul-19 21:30 
GeneralRe: adding double quote around variable add one backslash which causing issue Pin
Richard Deeming1-Aug-19 1:30
mveRichard Deeming1-Aug-19 1:30 
GeneralRe: adding double quote around variable add one backslash which causing issue Pin
Mou_kol1-Aug-19 5:44
Mou_kol1-Aug-19 5:44 
QuestionIs the "using" statement really a good idea? Pin
MSBassSinger30-Jul-19 7:19
professionalMSBassSinger30-Jul-19 7:19 
AnswerRe: Is the "using" statement really a good idea? Pin
Richard Deeming30-Jul-19 7:42
mveRichard Deeming30-Jul-19 7:42 
GeneralRe: Is the "using" statement really a good idea? Pin
MSBassSinger30-Jul-19 8:05
professionalMSBassSinger30-Jul-19 8:05 
GeneralRe: Is the "using" statement really a good idea? Pin
Richard Deeming30-Jul-19 8:15
mveRichard Deeming30-Jul-19 8:15 
MSBassSinger wrote:
No need for the nested try-finally blocks.
Without them, you can easily introduce bugs where resources don't get cleaned up.

For example:
C#
SqlConnection connection = new SqlConnection("...");
SqlCommand command = new SqlCommand("...", connection);
try
{
   ...
}
finally
{
    if (command != null) ((IDisposable)command).Dispose();
    if (connection != null) ((IDisposable)connection).Dispose();
}
If the SqlCommand ctor throws an exception, the SqlConnection is never cleaned up.

To do it properly, you would need something like:
C#
SqlConnection connection = null;
SqlCommand command = null;
try
{
    connection = new SqlConnection("...");
    command = new SqlCommand("...", connection);
    ...
}
finally
{
    if (command != null) ((IDisposable)command).Dispose();
    if (connection != null) ((IDisposable)connection).Dispose();
}
It's quite a lot of boilerplate and ceremony that the developer needs to get just right to make it work as well as a couple of using blocks.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Is the "using" statement really a good idea? Pin
MSBassSinger30-Jul-19 8:26
professionalMSBassSinger30-Jul-19 8:26 
AnswerRe: Is the "using" statement really a good idea? Pin
Dave Kreskowiak30-Jul-19 8:03
mveDave Kreskowiak30-Jul-19 8:03 
GeneralRe: Is the "using" statement really a good idea? Pin
MSBassSinger30-Jul-19 8:07
professionalMSBassSinger30-Jul-19 8:07 
GeneralRe: Is the "using" statement really a good idea? Pin
Dave Kreskowiak30-Jul-19 8:21
mveDave Kreskowiak30-Jul-19 8:21 
GeneralRe: Is the "using" statement really a good idea? Pin
MSBassSinger30-Jul-19 8:47
professionalMSBassSinger30-Jul-19 8:47 
GeneralRe: Is the "using" statement really a good idea? Pin
Dave Kreskowiak30-Jul-19 13:16
mveDave Kreskowiak30-Jul-19 13:16 
AnswerRe: Is the "using" statement really a good idea? Pin
OriginalGriff30-Jul-19 8:06
mveOriginalGriff30-Jul-19 8:06 
GeneralRe: Is the "using" statement really a good idea? Pin
MSBassSinger30-Jul-19 8:19
professionalMSBassSinger30-Jul-19 8:19 
GeneralRe: Is the "using" statement really a good idea? Pin
OriginalGriff30-Jul-19 9:44
mveOriginalGriff30-Jul-19 9:44 
GeneralRe: Is the "using" statement really a good idea? Pin
MSBassSinger30-Jul-19 9:50
professionalMSBassSinger30-Jul-19 9:50 
GeneralRe: Is the "using" statement really a good idea? Pin
Dave Kreskowiak30-Jul-19 13:19
mveDave Kreskowiak30-Jul-19 13:19 
AnswerRe: Is the "using" statement really a good idea? Pin
MSBassSinger30-Jul-19 10:09
professionalMSBassSinger30-Jul-19 10:09 
AnswerRe: Is the "using" statement really a good idea? Pin
Gerry Schmitz31-Jul-19 4:32
mveGerry Schmitz31-Jul-19 4:32 
Questioncapture the screenshot of desktop using C# Pin
Member 1454529529-Jul-19 22:41
Member 1454529529-Jul-19 22:41 
AnswerRe: capture the screenshot of desktop using C# Pin
OriginalGriff29-Jul-19 23:26
mveOriginalGriff29-Jul-19 23:26 
AnswerRe: capture the screenshot of desktop using C# Pin
Afzaal Ahmad Zeeshan30-Jul-19 5:29
professionalAfzaal Ahmad Zeeshan30-Jul-19 5:29 
QuestionCreating a struct dynamically Pin
Member 1452625729-Jul-19 21:48
Member 1452625729-Jul-19 21:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.