using-statement

Best practice for nested using statements?

半腔热情 提交于 2019-12-01 15:17:46
I have a code block as follows and I'm using 3 nested using blocks. I found that using try finally blocks I can avoid this but if there are more than two using statements, what is the best approach? private FileStream fileStream = null; private Document document = null; private PdfWriter pdfWriter = null; using (fileStream = new FileStream("ABC.pdf", FileMode.Create)) { using (document = new Document(PageSize.A4, marginLeft, marginRight, marginTop, marginBottom)) { using (pdfWriter = PdfWriter.GetInstance(document, fileStream)) { document.AddAuthor(metaInformation["author"]); document

Best practice for nested using statements?

℡╲_俬逩灬. 提交于 2019-12-01 14:10:56
问题 I have a code block as follows and I'm using 3 nested using blocks. I found that using try finally blocks I can avoid this but if there are more than two using statements, what is the best approach? private FileStream fileStream = null; private Document document = null; private PdfWriter pdfWriter = null; using (fileStream = new FileStream("ABC.pdf", FileMode.Create)) { using (document = new Document(PageSize.A4, marginLeft, marginRight, marginTop, marginBottom)) { using (pdfWriter =

CA2202: Do not dispose objects multiple times

你离开我真会死。 提交于 2019-12-01 08:32:04
问题 I have a class like so... public class Class1 { public Class1() { byte[] plainText = new byte[1024]; using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { csEncrypt.Write(plainText, 0, plainText.Length); csEncrypt.FlushFinalBlock(); csEncrypt.Flush(); encrypted = msEncrypt.ToArray(); } } } public ICryptoTransform encryptor { get; set; } public byte[] encrypted { get; set; } } Code analysis throws

Using statement question

醉酒当歌 提交于 2019-12-01 06:10:42
I have two questions. 1) Should you always use a using statement on a connection? So, I would use it on the connection and then another one on a reader within the connection? So I would be using two using statements. 2) Lets say you use the using statement on the connection and also a reader being returned on the connection. So you have two using statements. Does it create two Try{}Finally{} blocks or just one? Thanks! You should always use a using statement when an object implements IDisposable . This includes connections. It will create two nested try{}finally{} blocks. Be careful here. You

What is the purpose of Using? [duplicate]

会有一股神秘感。 提交于 2019-12-01 04:00:21
问题 This question already has answers here : Closed 10 years ago . DUPE: Uses of "using" in C# I have seen people use the following and I am wondering what is its purpose? Is it so the object is destroyed after its use by garbage collection? Example: using (Something mySomething = new Something()) { mySomething.someProp = "Hey"; } 回答1: Using translates, roughly, to: Something mySomething = new Something(); try { something.someProp = "Hey"; } finally { if(mySomething != null) { mySomething.Dispose

Using statement question

偶尔善良 提交于 2019-12-01 03:56:33
问题 I have two questions. 1) Should you always use a using statement on a connection? So, I would use it on the connection and then another one on a reader within the connection? So I would be using two using statements. 2) Lets say you use the using statement on the connection and also a reader being returned on the connection. So you have two using statements. Does it create two Try{}Finally{} blocks or just one? Thanks! 回答1: You should always use a using statement when an object implements

why is there no Dispose method on HttpWebResponse

这一生的挚爱 提交于 2019-12-01 02:36:07
HttpWebReponse implements IDisposable interface, but why is there no Dispose method. It only contains Close method. Will be using pattern still available for this class? HttpWebResponse implements IDisposable interface explicitly. So you can call Dispose only when you cast HttpWebResponse to IDisposable . The Close method of HttpWebResponse calls Dispose internally. HttpWebResponse response = // assigned from somewhere IDisposable disposableResponse = response as IDisposable; disposableResponse.Dispose(); Since HttpWebResponse implements IDisposable you can use it with an using-statement.

why is there no Dispose method on HttpWebResponse

爱⌒轻易说出口 提交于 2019-11-30 22:16:51
问题 HttpWebReponse implements IDisposable interface, but why is there no Dispose method. It only contains Close method. Will be using pattern still available for this class? 回答1: HttpWebResponse implements IDisposable interface explicitly. So you can call Dispose only when you cast HttpWebResponse to IDisposable . The Close method of HttpWebResponse calls Dispose internally. HttpWebResponse response = // assigned from somewhere IDisposable disposableResponse = response as IDisposable;

Best practice regarding returning from using blocks [duplicate]

陌路散爱 提交于 2019-11-30 20:18:46
This question already has an answer here: returning in the middle of a using block 7 answers Which way is better practice: return a value from a method inside an using statement or declare a variable before, set it inside and return it after? public int Foo() { using(..) { return bar; } } or public int Foo() { var b = null; using(..) { b = bar; } return b; } I prefer the first example. Fewer variables, fewer lines of code, easier to follow, easier to maintain... public int Foo() { using(..) { return bar; } } Following the "less is more" principle (really just a variant of KISS ), the former.

Bad practice? Non-canon usage of c#'s using statement

a 夏天 提交于 2019-11-30 14:37:31
问题 C# has the using statement, specifically for IDisposable objects. Presumably, any object specified in the using statement will hold some sort of resource that should be freed deterministically. However, it seems to me that there are many designs in programming which have a single, definite beginning and end, but lack intrinsic language support. The using construct provides an opportunity to use the built in features of a code editor to, at least, clearly and naturally highlight the scope of