Skip to content

Commit ed32a71

Browse files
authored
Dispose of WebClient and Stream in examples (#8483)
1 parent 8e409fc commit ed32a71

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

snippets/cpp/VS_Snippets_Remoting/NCLWebClientUserAgent/CPP/useragent.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ int main()
2525
Console::WriteLine( s );
2626
data->Close();
2727
reader->Close();
28+
delete client;
2829
}
2930

3031
//</Snippet1>

snippets/csharp/System.Net/WebClient/Overview/useragent.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55

66
public class Test
77
{
8-
public static void Main (string[] args)
8+
public static void Main(string[] args)
99
{
1010
if (args == null || args.Length == 0)
1111
{
12-
throw new ApplicationException ("Specify the URI of the resource to retrieve.");
12+
throw new ApplicationException("Specify the URI of the resource to retrieve.");
1313
}
14-
WebClient client = new WebClient ();
14+
using WebClient client = new WebClient();
1515

1616
// Add a user agent header in case the
1717
// requested URI contains a query.
1818

19-
client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
19+
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
2020

21-
Stream data = client.OpenRead (args[0]);
22-
StreamReader reader = new StreamReader (data);
23-
string s = reader.ReadToEnd ();
24-
Console.WriteLine (s);
25-
data.Close ();
26-
reader.Close ();
21+
using Stream data = client.OpenRead(args[0]);
22+
StreamReader reader = new StreamReader(data);
23+
string s = reader.ReadToEnd();
24+
Console.WriteLine(s);
25+
data.Close();
26+
reader.Close();
2727
}
2828
}
2929
//</Snippet1>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net6.0</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
</Project>

snippets/visualbasic/VS_Snippets_Remoting/NCLWebClientUserAgent/VB/useragent.vb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ Public Class Test
1010
If args Is Nothing OrElse args.Length = 0 Then
1111
Throw New ApplicationException("Specify the URI of the resource to retrieve.")
1212
End If
13-
Dim client As New WebClient()
14-
15-
' Add a user agent header in case the
16-
' requested URI contains a query.
17-
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
18-
19-
Dim data As Stream = client.OpenRead(args(0))
20-
Dim reader As New StreamReader(data)
21-
Dim s As String = reader.ReadToEnd()
22-
Console.WriteLine(s)
23-
data.Close()
24-
reader.Close()
13+
Using client As New WebClient()
14+
15+
' Add a user agent header in case the
16+
' requested URI contains a query.
17+
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
18+
19+
Dim data As Stream = client.OpenRead(args(0))
20+
Dim reader As New StreamReader(data)
21+
Dim s As String = reader.ReadToEnd()
22+
Console.WriteLine(s)
23+
data.Close()
24+
reader.Close()
25+
End Using
2526
End Sub
2627
End Class
2728
'</Snippet1>

0 commit comments

Comments
 (0)