using-statement

Trying to understand the 'using' statement better

99封情书 提交于 2019-12-19 19:19:19
问题 I have read a couple of articles about the using statement to try and understand when it should be used. It sound like most people reckon it should be used as much as possible as it guarantees disposal of unused objects. Problem is that all the examples always show something like this: using (SqlCommand scmFetch = new SqlCommand()) { // code } That makes sense, but it's such a small piece of code. What should I do when executing a query on a database? What are all the steps? Will it look

Trying to understand the 'using' statement better

99封情书 提交于 2019-12-19 19:18:06
问题 I have read a couple of articles about the using statement to try and understand when it should be used. It sound like most people reckon it should be used as much as possible as it guarantees disposal of unused objects. Problem is that all the examples always show something like this: using (SqlCommand scmFetch = new SqlCommand()) { // code } That makes sense, but it's such a small piece of code. What should I do when executing a query on a database? What are all the steps? Will it look

Nesting 'IDisposable's in a single 'using' statement

不打扰是莪最后的温柔 提交于 2019-12-19 18:22:25
问题 Quick question about using nested disposables in a single 'using' statement: Should I write out each disposable's using statement, or can I nest them into one? Example: using( FileStream inFile = new FileStream( "myFile.txt", FileMode.Open ) ) using( GZipStream gzip = new GZipStream( inFile, CompressionMode.Decompress ) ) using( FileStream outFile = new FileStream( "myNewFile.txt", FileMode.CreateNew ) ) { gzip.CopyTo( outstream ); } vs. using( GZipStream gzip = new GZipStream( new FileStream

Which is better, and when: using statement or calling Dispose() on an IDisposable in C#?

送分小仙女□ 提交于 2019-12-18 06:14:48
问题 Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ... } Why not just do the following and lose a couple of curly braces?: var ctx = DataContextFactory.Create(0); ctx.Dispose(); Thanks for the advice! 回答1: The first is better. It ensures it is disposed even if an exception is thrown, and it correctly handles the case where Create(0) returns null (i.e. it doesn't attempt to call Dispose() on a null instance). 回答2: A using statement is always better

Which is better, and when: using statement or calling Dispose() on an IDisposable in C#?

落花浮王杯 提交于 2019-12-18 06:13:03
问题 Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ... } Why not just do the following and lose a couple of curly braces?: var ctx = DataContextFactory.Create(0); ctx.Dispose(); Thanks for the advice! 回答1: The first is better. It ensures it is disposed even if an exception is thrown, and it correctly handles the case where Create(0) returns null (i.e. it doesn't attempt to call Dispose() on a null instance). 回答2: A using statement is always better

Is there a list of common object that implement IDisposable for the using statement?

╄→гoц情女王★ 提交于 2019-12-18 04:33:29
问题 I was wondering if there was some sort of cheat sheet for which objects go well with the using statement... SQLConnection , MemoryStream , etc. Taking it one step further, it would be great to even show the other "pieces of the puzzle", like how you should actually call connection.Close() before the closing using statement bracket. Anything like that exist? If not, maybe we should make one. 回答1: Microsoft FxCop has a rule checking that you use an IDisposbale in a using block. 回答2: Perhaps

C# “Using” Syntax

自作多情 提交于 2019-12-17 15:47:28
问题 Does the using catch the exception or throw it? i.e. using (StreamReader rdr = File.OpenText("file.txt")) { //do stuff } If the streamreader throws an exception is it caught by using or thrown so the calling function can handle it? 回答1: using statements do not eat exceptions. All "Using" does is scope your object to the using block, and automatically calls Dispose() on the object when it leaves the block. There is a gotcha though, if a thread is forcefully aborted by an outside source, it is

Why use a using statement with a SqlTransaction?

删除回忆录丶 提交于 2019-12-17 10:21:11
问题 I've been running into some problems concerning a SqlTransaction I'm using in my code. During my Googling I see many people using a using statement with a SqlTransaction. What is the benefit and/or difference of using this type of statement with a SqlTransaction? using (SqlConnection cn = new SqlConnection()) { using (SqlTransaction tr = cn.BeginTransaction()) { //some code tr.Commit(); } } Currently my code looks like this: SqlConnection cn = new SqlConnection(ConfigurationManager

SqlCommand with using statement

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 06:54:02
问题 I saw that in most samples SqlCommand was used like this using (SqlConnection con = new SqlConnection(CNN_STRING)) { using (SqlCommand cmd = new SqlCommand("Select ID,Name From Person", con)) { SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); return ds; } } I know why we are using "using" statement. But SqlCommand doesn't inlcude Close() method, so should we really use it within using statement 回答1: Because it also implements IDisposable. The purpose of

SqlCommand with using statement

拥有回忆 提交于 2019-12-17 06:52:19
问题 I saw that in most samples SqlCommand was used like this using (SqlConnection con = new SqlConnection(CNN_STRING)) { using (SqlCommand cmd = new SqlCommand("Select ID,Name From Person", con)) { SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); return ds; } } I know why we are using "using" statement. But SqlCommand doesn't inlcude Close() method, so should we really use it within using statement 回答1: Because it also implements IDisposable. The purpose of