Autor Beitrag
Talemantros
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 444
Erhaltene Danke: 2

Win7 Proff 64bit
C# (VS2013)
BeitragVerfasst: So 23.03.14 12:48 
Guten Morgen,
im Laufe der Suche im Internet zu Problemstellungen finde ich immer wieder using-Anweisungen

ausblenden C#-Quelltext
1:
using (SqlConnection conn = new SqlConnection(connectionString))					


Während manchmal auch

ausblenden C#-Quelltext
1:
SqlConnection conn = new SqlConnection(connectionString)					


scheinbar das selbe tut.


Finde aber irgendwie keine Erklärung.

Vielen Dank

Gruß
Daniel


Moderiert von user profile iconTh69: Topic aus Sonstiges (.NET) verschoben am So 23.03.2014 um 12:49
Yankyy02
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 138
Erhaltene Danke: 21

Win 11 x64
C# (VS 2022 - Rider)
BeitragVerfasst: So 23.03.14 13:25 
Hallo,

schau dir mal diese Erklärung dazu an dann dürfte dir einiges klarer sein was das using hier zu bedeuten hat.
www.codingfreaks.de/...sposable_verstanden/


MfG

Gery

_________________
the quiter you become, the more you are able to hear
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: So 23.03.14 13:49 
Hallo,

das Entscheidende wird aber in dem Artikel nicht erwähnt (sondern nur in einem der Kommentare), nämlich daß bei using(...) die Dispose()-Methode auch im Falle einer Exception aufgerufen wird, d.h. technisch entspricht der using (X x = new X())-Anweisung:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
try
{
   X x = new X();
}
finally
{
   ((IDisposable)x).Dispose();
}

Grundsätzlich sollte also bei der Benutzung jeder Klasse, welche das IDisposable-Interface implementiert, using(...) verwendet werden.
Talemantros Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 444
Erhaltene Danke: 2

Win7 Proff 64bit
C# (VS2013)
BeitragVerfasst: So 23.03.14 16:15 
Vielen Dank