using-statement

IDisposable.Dispose is never called after exception in using block

我的梦境 提交于 2019-12-02 05:52:44
问题 I understand from many sources like this and this that the Dispose method of an IDisposable will always be called if an exception is thrown in a Using block. So then I have this code: static class MainEntryPoint { static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException; using (var x = new Disposable()) { throw new Exception("asdfsdf"); } } private static void HandleUnhandledException(Object sender, System.UnhandledExceptionEventArgs e) {

Benefit/Use of using statement before streamwriter , streamreader [duplicate]

我怕爱的太早我们不能终老 提交于 2019-12-02 05:46:54
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: What is the C# Using block and why should I use it? So I've just noticed that at msdn examples and some stackoverflow questions there were answer where the using statement is used before the streamwriter etc, but what is actually the benefit? Since I've never been taught/told/read any reason to use it. using (StreamReader sr = new StreamReader(path)) { while (sr.Peek() >= 0) Console.WriteLine(sr.ReadLine()); }

IDisposable.Dispose is never called after exception in using block

本小妞迷上赌 提交于 2019-12-02 01:50:15
I understand from many sources like this and this that the Dispose method of an IDisposable will always be called if an exception is thrown in a Using block. So then I have this code: static class MainEntryPoint { static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException; using (var x = new Disposable()) { throw new Exception("asdfsdf"); } } private static void HandleUnhandledException(Object sender, System.UnhandledExceptionEventArgs e) { Environment.Exit(0); } } class Disposable : IDisposable { public void Dispose() { System.Diagnostics.Debug

Do I need to use multiple using statements?

泪湿孤枕 提交于 2019-12-02 01:38:32
Both classes for practicality sake are disposable. I understand what a using block does. But I'm not sure of all of the ways it can or needs to be used. For example is this correct? using (MyClass myClass = new MyClass(params)) { myClass.name = "Steve"; SecondClass myClassSecond = new SecondClass(params); myClassSecond.name = "George"; myClassSecond.msg = "Hello Man in the Yellow Hat"; } Are both classes above disposed of? Or do I need both inside a using statement? using (MyClass myClass = new MyClass(params)) { myClass.name = "Steve"; using (SecondClass myClassSecond = new SecondClass(params

Benefit/Use of using statement before streamwriter , streamreader [duplicate]

走远了吗. 提交于 2019-12-02 01:19:46
Possible Duplicate: What is the C# Using block and why should I use it? So I've just noticed that at msdn examples and some stackoverflow questions there were answer where the using statement is used before the streamwriter etc, but what is actually the benefit? Since I've never been taught/told/read any reason to use it. using (StreamReader sr = new StreamReader(path)) { while (sr.Peek() >= 0) Console.WriteLine(sr.ReadLine()); } Instead of: StreamReader sr = new StreamReader(path); while (sr.Peek() >= 0) Console.WriteLine(sr.ReadLine()); The using block calls the Dispose method of the object

What's the value in removing and/or sorting Usings?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 22:14:24
I've always run Remove and Sort Usings as a matter of course, because it seems the right thing to do. But just now I got to wondering: Why do we do this? Certainly, there's always a benefit to clean & compact code. And there must be some benefit if MS took the time to have it as a menu item in VS. Can anyone answer: why do this? What are the compile-time or run-time (or other) benefits from removing and/or sorting usings? As @craig-w mentions, there's a very small compile time performance improvement. The way that the compiler works, is when it encounters a type, it looks in the current

Trying to understand the 'using' statement better

北战南征 提交于 2019-12-01 19:02:29
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 something like this: string sQuery = @" SELECT [ID], [Description] FROM [Zones] ORDER BY [Description] ";

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

这一生的挚爱 提交于 2019-12-01 17:16:36
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( "myFile.txt", FileMode.Open ), CompressionMode.Decompress ) ) using( FileStream outFile = new

Does connection close when command is disposed and the connection is defined directly on the command?

本秂侑毒 提交于 2019-12-01 16:18:21
I know that a lot of examples exist where a SqlConnection is defined and then a SqlCommand is defined, both in Using blocks: using (var conn = new SqlConnection(connString)) { using (var cmd = new SqlCommand()) { cmd.Connection = conn; //open the connection } } My question: If I define the connection directly on the SqlCommand, does the connection close when the command is disposed? using (var cmd = new SqlCommand()) { cmd.Connection = new SqlConnection(connString); //open the connection } No, SqlCommand never attempts to close/dispose of the connection. No, the connection object will not be

Does connection close when command is disposed and the connection is defined directly on the command?

空扰寡人 提交于 2019-12-01 15:57:53
问题 I know that a lot of examples exist where a SqlConnection is defined and then a SqlCommand is defined, both in Using blocks: using (var conn = new SqlConnection(connString)) { using (var cmd = new SqlCommand()) { cmd.Connection = conn; //open the connection } } My question: If I define the connection directly on the SqlCommand, does the connection close when the command is disposed? using (var cmd = new SqlCommand()) { cmd.Connection = new SqlConnection(connString); //open the connection }