Click here to Skip to main content
15,889,867 members
Home / Discussions / C#
   

C#

 
QuestionWhy Using during Sql Server Connection Pin
munishk14-Jul-12 3:11
munishk14-Jul-12 3:11 
AnswerRe: Why Using during Sql Server Connection Pin
Eddy Vluggen14-Jul-12 3:35
professionalEddy Vluggen14-Jul-12 3:35 
GeneralRe: Why Using during Sql Server Connection Pin
jschell14-Jul-12 6:55
jschell14-Jul-12 6:55 
GeneralRe: Why Using during Sql Server Connection Pin
munishk14-Jul-12 18:59
munishk14-Jul-12 18:59 
GeneralRe: Why Using during Sql Server Connection Pin
PIEBALDconsult15-Jul-12 7:30
mvePIEBALDconsult15-Jul-12 7:30 
GeneralRe: Why Using during Sql Server Connection Pin
jschell15-Jul-12 7:30
jschell15-Jul-12 7:30 
AnswerRe: Why Using during Sql Server Connection Pin
Wes Aday14-Jul-12 3:36
professionalWes Aday14-Jul-12 3:36 
AnswerRe: Why Using during Sql Server Connection PinPopular
OriginalGriff14-Jul-12 3:43
mveOriginalGriff14-Jul-12 3:43 
using is not just for unmanaged code - in fact I almost never write unmanaged code, and I use using all the time.

What is does is ensure that the object created in the using block header is Disposed at the end, exactly as if it had been written as
C#
MyObject mo = new MyObject();
...
mo.Dispose():
In fact, a using block is just a syntactic sugar for just that, but with the added advantage that it terminates the scope of the variable as well, so you can't accidentally use the Disposed object.

Why do I use it? It's clean, it's clear, and it de-scopes a variable when I can't use it again.
Why is it important to Dispose managed objects? Because some of them hold resources: for example, Bitmaps, Files and so forth hang on to resources such as handles and file access locks until the object is disposed - in the case of a Bitmap it is not at all obvious that:
C#
Image i = Image.FromFile(path);
puts a file access lock on the image source file until the image is Disposed. So if you use this to load an image, let the user modify it and then try to save it again, the file may or may not still be in use...annoying. Surrounding the Image creating with a using block removes this problem.

If an object supports IDisposable, then Dispose should be called on it. And a using block is the easiest, cleanest way to do that.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

AnswerRe: Why Using during Sql Server Connection Pin
Luc Pattyn14-Jul-12 7:18
sitebuilderLuc Pattyn14-Jul-12 7:18 
AnswerRe: Why Using during Sql Server Connection Pin
PIEBALDconsult15-Jul-12 7:39
mvePIEBALDconsult15-Jul-12 7:39 
GeneralLooking for Volunteers Pin
Santa's Little Helper14-Jul-12 3:04
Santa's Little Helper14-Jul-12 3:04 
GeneralRe: Looking for Volunteers Pin
Eddy Vluggen14-Jul-12 3:37
professionalEddy Vluggen14-Jul-12 3:37 
GeneralRe: Looking for Volunteers Pin
Santa's Little Helper14-Jul-12 3:50
Santa's Little Helper14-Jul-12 3:50 
QuestionUI For C# Pin
atoi_powered13-Jul-12 10:23
atoi_powered13-Jul-12 10:23 
AnswerRe: UI For C# PinPopular
fjdiewornncalwe13-Jul-12 10:46
professionalfjdiewornncalwe13-Jul-12 10:46 
GeneralRe: UI For C# Pin
atoi_powered13-Jul-12 11:03
atoi_powered13-Jul-12 11:03 
AnswerRe: UI For C# Pin
Pete O'Hanlon13-Jul-12 12:20
mvePete O'Hanlon13-Jul-12 12:20 
GeneralRe: UI For C# Pin
atoi_powered13-Jul-12 12:56
atoi_powered13-Jul-12 12:56 
GeneralRe: UI For C# Pin
Mycroft Holmes13-Jul-12 13:37
professionalMycroft Holmes13-Jul-12 13:37 
GeneralRe: UI For C# Pin
Pete O'Hanlon13-Jul-12 22:11
mvePete O'Hanlon13-Jul-12 22:11 
QuestionC# - Isolated Storage Database Programming - Windows Phone Pin
ashish712-Jul-12 22:57
ashish712-Jul-12 22:57 
AnswerRe: C# - Isolated Storage Database Programming - Windows Phone Pin
Richard MacCutchan12-Jul-12 23:48
mveRichard MacCutchan12-Jul-12 23:48 
GeneralSp_attach_db Pin
pranee2412-Jul-12 6:34
pranee2412-Jul-12 6:34 
GeneralRe: Sp_attach_db Pin
Richard MacCutchan12-Jul-12 6:40
mveRichard MacCutchan12-Jul-12 6:40 
GeneralRe: Sp_attach_db Pin
Wes Aday12-Jul-12 6:47
professionalWes Aday12-Jul-12 6:47 

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.