using-statement

.NET/C# - Disposing an object with the 'using' statement

岁酱吖の 提交于 2019-12-09 16:11:53
问题 Suppose I have a method like so: public byte[] GetThoseBytes() { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { ms.WriteByte(1); ms.WriteByte(2); return ms.ToArray(); } } Would this still dispose the 'ms' object? I'm having doubts, maybe because something is returned before the statement block is finished. Thanks, AJ. 回答1: Yes. using (x = e) { s } is sugar for { x = e; try { s } finally { x.Dispose(); } } 回答2: Yes, Using creates a try..finally block, so it disposes the ms

When to Dispose?

☆樱花仙子☆ 提交于 2019-12-08 19:16:06
问题 I'm getting confused about all this talk about IDispose and "using" Statements. I wonder if someone can tell me if I need to use either a "using" Statement or some sort of implementation of IDispose in the following test example... public class Main() { MyFile myFile = new MyFile("c:\subdir\subdir2\testFile.txt"); Console.Writeline("File Name: " + myFile.FileName() + "File Size: " + myFile.FileSize()); } public class MyFile { private FileInfo _fInfo; public MyFile(string fullFilePath) {

Declare 2 types inside using statement gives compile error?

谁说胖子不能爱 提交于 2019-12-08 17:33:51
问题 I want to use this line of code: using (ADataContext _dc = new ADataContext(ConnectionString), BDataContext _dc2 = new BrDataContext(ConnectionString)){ // ...} This gives a compile error: Cannot use more than one type in a for, using, fixed or declartion statement. I thought this was possible? MSDN says it is: http://msdn.microsoft.com/en-us/library/yh598w02%28VS.80%29.aspx In the MSDN sample code Font is used, which is class and thereby a reference type as well as my two DataContext classes

using object available to view, unavailable to partial view

╄→гoц情女王★ 提交于 2019-12-08 06:38:11
问题 I have a view and a partial view. Within the controller action methods, I have a using statement which establishes some back-end threading context, including providing access to a model object for display in the view. The problem that I'm having is that while the context and the associated model object are available for the main view, they are disposed before the partial view is rendered. This seems to be the case regardless of whether I pass the model object from the view to the partial view

C#.NET using block

柔情痞子 提交于 2019-12-07 18:00:27
问题 I want to use the "using" block in my DAL layer. Like using (SqlConnection con = new SqlConnection("connection string")) { Command object Reader object } Since the SqlConnection object in initialised in the using block i know that this connection object will be automatically disposed when the control exits the using block scope. But i am creating Command and Reader objects inside the using block. Do i have to explicitly close them or do i have to write another "using" block for them. 回答1: You

I don't quite understand the workings of using/Disposable objects

折月煮酒 提交于 2019-12-07 02:33:51
问题 I asked a question regarding returning a Disposable (IDisposable) object from a function, but I thought that I would muddle the discussion if I raised this question there. I created some sample code: class UsingTest { public class Disposable : IDisposable { public void Dispose() { var i = 0; i++; } } public static Disposable GetDisposable(bool error) { var obj = new Disposable(); if (error) throw new Exception("Error!"); return obj; } } I coded it this way deliberately, because then I call:

What happens when 'return' is called from within a 'using' block? [duplicate]

穿精又带淫゛_ 提交于 2019-12-06 17:06:05
问题 This question already has answers here : returning in the middle of a using block (7 answers) Closed 6 years ago . If I have a method with a using block like this... public IEnumerable<Person> GetPersons() { using (var context = new linqAssignmentsDataContext()) { return context.Persons.Where(p => p.LastName.Contans("dahl")); } } ...that returns the value from within the using block, does the IDisposable object still get disposed? 回答1: Yes it does. The disposing of the object occurs in a

Exception in “using” statement with WCF not closing connections properly. How does one close faulted WCF client connections or those with exceptions?

戏子无情 提交于 2019-12-06 02:55:15
问题 There are several questions on StackOverflow regarding closing WCF connections, however the highest ranking answers refers to this blog: http://marcgravell.blogspot.com/2008/11/dontdontuse-using.html I have a problem with this technique when I set a breakpoint at the server and let the client hang for more than one minute. (I'm intentionally creating a timeout exception) The issue is that the client appears to "hang" until the server is done processing. My guess is that everything is being

Should '#include' and 'using' statements be repeated in both header and implementation files (C++)?

岁酱吖の 提交于 2019-12-06 02:13:47
问题 I'm fairly new to C++, but my understanding is that a #include statement will essentially just dump the contents of the #included file into the location of that statement. This means that if I have a number of '#include' and 'using' statements in my header file, my implementation file can just #include the header file, and the compiler won't mind if I don't repeat the other statements. What about people though? My main concern is that if I don't repeat the '#include', 'using', and also

C#.NET using block

做~自己de王妃 提交于 2019-12-06 01:10:35
I want to use the "using" block in my DAL layer. Like using (SqlConnection con = new SqlConnection("connection string")) { Command object Reader object } Since the SqlConnection object in initialised in the using block i know that this connection object will be automatically disposed when the control exits the using block scope. But i am creating Command and Reader objects inside the using block. Do i have to explicitly close them or do i have to write another "using" block for them. You should use using for the Command and Reader as well, or explicitly close them. I normally code it like this