using-statement

Uses of "using" in C#

User kokos answered the wonderful Hidden Features of C# question by mentioning the using keyword. Can you elaborate on that? What are good uses of using? ...

C# "Using" Syntax

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? ...

C# using statement catch error

I am just looking at the using statement, I have always known what it does but until now not tried using it, I have come up with the below code: using (SqlCommand cmd = new SqlCommand(reportDataSource, new SqlConnection(Settings.Default.qlsdat_extensionsConnectionString))) { cmd.CommandType = CommandType.StoredPro...

Dealing with .NET IDisposable objects

I work in C#, and I've been pretty lax about using using blocks to declare objects that implement IDisposable, which you're apparently always supposed to do. However, I don't see an easy way of knowing when I'm slipping up. Visual Studio doesn't seem to indicate this in any way (am I just missing something?). Am I just supposed to che...

using statement vs try finally

I've got a bunch of properties which I am going to use read/write locks on. I can implement them either with a try finally or a using clause. In the try finally I would acquire the lock before the try, and ralease in the finally. In the using clause, I would create a class which acquires the lock in its constructor, and releases in its...

What is the Managed C++ equivalent to the C# using statement

How would one code the following C# code in Managed C++ void Foo() { using (SqlConnection con = new SqlConnection("connectionStringGoesHere")) { //do stuff } } Clarificaton: For managed objects. ...

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

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 direct...

Is there a quick way to remove using statements in C#?

Is there a quick way to determine whether you are using certain namespaces in your application. I want to remove all the unneccessary using statements like using System.Reflection and so on, but I need a way to determine if I am using those libraries or not. I know that the tool Resharper does this for you, but is there a quick and dirty...

C# network programming and resource usage

I've been doing a lot of research on how best to write "correct" network code in C#. I've seen a number of examples using the "using" statement of C#, and I think this is a good approach, however i've seen inconsistent use of it with various expressions. For instance, suppose I have some code like this: TcpClient tcpClient = new TcpCl...

Does Dispose method still get called when Exception is thrown inside of Using statment?

In the example below, is connection going to close and dispose when exception is thrown if it is within using statement? using (var conn = new SqlConnection("...")) { conn.Open(); // stuff happens here and exception is thrown... } I know this code below will make sure that it does, but I'm curious how using statment does it. ...

Is it acceptable to add a "using namespace" immediately after the namespace declaration?

I have a small namespace containing some type definitions, which I use to make my code look cleaner. However I don't want to have to add a "using namespace ..." line to every file that uses one of these types, after all I already have to add a #include for the file. MyFile.cpp: #include "typedefs.h" void Bob() { IntList^ list = gcne...

What is the purpose of Using?

DUPE: http://stackoverflow.com/questions/75401/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"; } ...

Ab-using languages

Some time ago I had to address a certain C# design problem when I was implementing a JavaScript code-generation framework. One of the solutions I came with was using the “using” keyword in a totally different (hackish, if you please) way. I used it as a syntax sugar (well, originally it is one anyway) for building hierarchical code struc...

Can a return statement prevent a using statement from closing a connection to a database?

When I'm creating temp tables I get an error message telling me that the temp table already exists. The temp table is unique to the session so it seems my connection isn't closing properly and I think it may have something to do with a return statement I have in my using statement. I have the following code: using (IDbConnection connec...

Identify IDisposable objects

Hi, i have to review a code made by some other person that has some memory leaks. Right now i'm searching the disposable objects to enclause them with the using statement and i would like to know if there is a quick way that tells you all the disposable objects declared in. I mean something like resharper or another visual studio plugin....

Will a using statement rollback a database transaction if an error occurs?

I've got an IDbTransaction in a using statement but I'm unsure if it will be rolled back if an exception is thrown in a using statement. I know that a using statement will enforce the calling of Dispose()...but does anyone know if the same is true for Rollback()? Update: Also, do I need to call Commit() explicitly as I have below or wil...

What is the point of having using blocks in C# code?

I see loads of code snippets with the following Syntax using (RandomType variable = new RandomType(1,2,3)) { // A bunch of code here. } why not just declare the variable and use it? This Using syntax seems to just clutter the code and make it less readable. And if it's so important that that varible is only available in that scop...

returning in the middle of a using block.

Something like: using (IDisposable disposable = GetSomeDisposable()) { //..... //...... return Stg(); } I believe it is not a proper place for a return statement, is it? ...

Is an object still disposed if I return within a using statement?

Dupe of http://stackoverflow.com/questions/662773/returning-in-the-middle-of-a-using-block Question title is fairly obvious I think, so given the following code is the SecurityDisabler disposed if true is returned? public bool CreateProxyItem(string name, Sitecore.Data.ID sourceID, Sitecore.Data.ID targetID) { // create an instance...

How do I know the best place to use 'using'?

I'm somewhat new to c#, more accustomed to scripting languages. I like the idea of 'using', you instantiate an object, and then you operate within its scope as long as you need it, then you let it dispose of itself when it's done its purpose. But, it's not natural for me. When people show me examples using it, I recognize it as a good...