I've read and I believe I understand what C#'s using statement does (please correct me if I'm wrong): Initializes an IDisposable object as read only to a limited scope (the using block). I know you can initialize before the using and that doesn't limit the scope, but that is advised against here:
http://msdn.microsoft.com/en-us/library/...
I've the following code
using(MemoryStream ms = new MemoryStream())
{
//code
return 0;
}
The dispose() method is called at the end of using statement braces } right? Since I return before the end of the using statement, will the MemoryStream object be disposed properly? What happens here?
...
Hi,
I was reading about this scenario where making use of the C# using statement can cause problems. Exceptions thrown within the scope of the using block can be lost if the Dispose function called at the end of the using statement was to throw an exception also. This highlights that care should be taken in certain cases when deciding w...
Can exceptions be caught inside a using block, and if so what is the syntax?
So, something like the following:
using (var creatingThing = new MyCreatingThing())
{
creatingThing.CreateSomething();
catch()
{
creatingThing.Rollback();
}
}
Can this be done? Or do I need to write this code manually (ie without a ...
using(SomeClass x = new SomeClass("c:/temp/test.txt"))
{
...
}
Inside the using block, all is fine with treating exceptions as normal. But what if the constructor of SomeClass can throw an exception?
...
In my DAL I write queries like this:
using(SQLConnection conn = "connection string here")
{
SQLCommand cmd = new ("sql query", conn);
// execute it blah blah
}
Now it just occurred to me that I am not explicitly closing the SQLCommand object. Now I know the 'using' block will take care of the SQLConnection object, but will thi...
Hi guys,
I know I asked a related question earlier. I just had another thought.
using (SqlConnection conn = new SqlConnection('blah blah'))
{
using(SqlCommand cmd = new SqlCommand(sqlStatement, conn))
{
conn.open();
SqlDataReader dr = cmd.ExecuteReader() //do I need to put this in using as well?
{
...
Could you help me and tell if im using the "using statement" correctly in my directoryservice function that gets distingushed name from my Active Directory. I want to dispose and close objects correctly.
Code:
Public Function GetObjectDistinguishedName(ByVal objClass As objectClass, _
ByVal returnValue As returnType, _
ByVa...
I want to know how to safely call a WCF web service method. Are both of these methods acceptable/equivalent? Is there a better way?
1st way:
public Thing GetThing()
{
using (var client = new WebServicesClient())
{
var thing = client.GetThing();
return thing;
}
}
2nd way:
public Thing GetThing()
{
We...
Is it possible to have additional "using" statements automatically added to my new aspx.cs files so that I do not have to keep typing the same ones over and over again (i.e. custom namespace using statements)
...
What is better, the using directive, or the dispose directive when finished with an object?
using(FileStream fileStream = new FileStream(
"logs/myapp.log",
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
{
using(StreamReader streamReader = new StreamReader(fileSt...
Say you have some boiler plate using statements. Say something like this:
#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Category = Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framew...
I use the using statement for SqlConnection. It's is good for performance because forces calling Dispose() that simply releases the connection to the pool sooner.
However, I realized that object created in using cannot be redefined. I cannot do like this:
using (SqlConnection connection = new SqlConnection(connectionString))
{
...
Consider this simple query:
SELECT * FROM table1 JOIN table2 USING(pid) WHERE pid='2' ;
I get this error:
SELECT command denied to user 'root'@'localhost' for column 'pid' in table 'table1'
When I replace USING with ON (and this right syntax...) the error disappears.
What is the problem?
...
Note: Object Lifetime RAII not using/with block scope RAII
It seems like its possible using an extra gc category, short lived objects(check gc category somewhat frequently), long lived objects(check gc category less frequently), and resource objects(check gc category very frequently). Or possibly with an extra reference counting gc for ...
When it comes to using statements in C# (not to be confused with using directives that import namespaces), Visual Studio doesn't indent single-line code that follows if no braces are employed. This is typical of "nesting" using statements as shown in this SO question.
I find it confusing that subsequent statements after using are not in...
Most of the research I've done on the use of using declarations, including reading relevant sections of various style guides, indicates that whether or not to use using declarations in C++ source files, as long as they appear after all #includes, is a decision left to the coder. Even the style guides I read, which usually come down on on...
The project that I'm working on at the moment uses an IDisposable object in every method in a class. It has started getting tedious re-typing the using block at the start of every method, and was wondering if there was a way to specify a disposable variable for use in every method of the class?
public static class ResourceItemRepository...
I've just finished reading "C# 4.0 in a Nutshell" (O'Reilly) and I think it's a great book for a programmer willing to switch to C#, but it left me wondering. My problem is the definition of using statement. According to the book (p. 138),
using (StreamReader reader = File.OpenText("file.txt")) {
...
}
is precisely equivalent to:
...
I have a class that includes an enum:
class appearance{
// ... stuff ...
enum color {BLUE, RED, GREEN};
};
I would like to attach part of the namespace (with using) so that I can refer to the value of the BLUE simply as BLUE, rather than appearance::BLUE. At the same time, I would like to keep the enum within the class{}, since I ...