using-statement

Does Java have a using statement?

百般思念 提交于 2019-11-26 08:37:26
问题 Does Java have a using statement that can be used when opening a session in hibernate? In C# it is something like: using (var session = new Session()) { } So the object goes out of scope and closes automatically. 回答1: Java 7 introduced Automatic Resource Block Management which brings this feature to the Java platform. Prior versions of Java didn't have anything resembling using . As an example, you can use any variable implementing java.lang.AutoCloseable in the following way: try

Does Dispose still get called when exception is thrown inside of a using statement?

别说谁变了你拦得住时间么 提交于 2019-11-26 08:15:16
问题 In the example below, is the connection going to close and disposed when an exception is thrown if it is within a 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 statement does it. var conn; try { conn = new SqlConnection(\"...\"); conn.Open(); // stuff happens here and exception is thrown... } // catch it or let it bubble up finally {

try/catch + using, right syntax

天大地大妈咪最大 提交于 2019-11-26 03:49:32
问题 Which one: using (var myObject = new MyClass()) { try { // something here... } catch(Exception ex) { // Handle exception } } OR try { using (var myObject = new MyClass()) { // something here... } } catch(Exception ex) { // Handle exception } 回答1: I prefer the second one. May as well trap errors relating to the creation of the object as well. 回答2: Since a using block is just a syntax simplification of a try/finally (MSDN), personally I'd go with the following, though I doubt it's significantly

How to use an iterator?

北慕城南 提交于 2019-11-26 03:06:57
问题 I\'m trying to calculate the distance between two points. The two points I stored in a vector in C++: (0,0) and (1,1). I\'m supposed to get results as 0 1.4 1.4 0 But the actual result that I got is 0 1 -1 0 I think there\'s something wrong with the way I use iterator in vector. How can I fix this problem? I posted the code below. typedef struct point { float x; float y; } point; float distance(point *p1, point *p2) { return sqrt((p1->x - p2->x)*(p1->x - p2->x) + (p1->y - p2->y)*(p1->y - p2-

'using' statement vs 'try finally'

与世无争的帅哥 提交于 2019-11-26 02:35:25
问题 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 release in the finally . In the using clause, I would create a class which acquires the lock in its constructor, and releases in its Dispose method. I\'m using read/write locks in a lot of places, so I\'ve been looking for ways that might be more concise than try finally . I\'m interested

What is the C# Using block and why should I use it? [duplicate]

女生的网名这么多〃 提交于 2019-11-25 22:57:50
问题 This question already has answers here : What are the uses of “using” in C# (29 answers) Closed 4 years ago . What is the purpose of the Using block in C#? How is it different from a local variable? 回答1: If the type implements IDisposable, it automatically disposes it. Given: public class SomeDisposableType : IDisposable { ...implmentation details... } These are equivalent: SomeDisposableType t = new SomeDisposableType(); try { OperateOnType(t); } finally { if (t != null) { ((IDisposable)t)

The type or namespace name could not be found [duplicate]

余生颓废 提交于 2019-11-25 22:43:14
问题 This question already has an answer here: Getting “type or namespace name could not be found” but everything seems ok? 33 answers I have a C# solution with several projects in Visual Studio 2010 . One is a test project (I\'ll call it \" PrjTest \"), the other is a Windows Forms Application project (I\'ll call it \" PrjForm \"). There is also a third project referenced by PrjForm, which it is able to reference and use successfully. PrjForm references PrjTest , and PrjForm has a class with a

What are the uses of “using” in C#

老子叫甜甜 提交于 2019-11-25 22:18:50
问题 User kokos answered the wonderful Hidden Features of C# question by mentioning the using keyword. Can you elaborate on that? What are the uses of using ? 回答1: The reason for the using statement is to ensure that the object is disposed as soon as it goes out of scope, and it doesn't require explicit code to ensure that this happens. As in Understanding the 'using' statement in C# , the C# compiler converts using (MyResource myRes = new MyResource()) { myRes.DoSomething(); } to { // Limits