A using block without curly braces or semi-colon has an implied body:
public Task<string> GetElidingKeywordsAsync(string url)
{
using (var client = new HttpClient())
return client.GetStringAsync(url); // using body
}
This can be normalized to:
public Task<string> GetElidingKeywordsAsync(string url)
{
using (var client = new HttpClient())
{
return client.GetStringAsync(url);
}
}
Or written more compactly in C#8.0:
public Task<string> GetElidingKeywordsAsync(string url)
{
using var client = new HttpClient();
return client.GetStringAsync(url);
}
If you add a semi-colon, there would be an empty body, yielding the behavior you describe in OP:
public Task<string> GetElidingKeywordsAsync(string url)
{
HttpClient client;
using (client = new HttpClient()); // gets disposed before next statement
return client.GetStringAsync(url); // don't be fooled by the indent
}
This can be normalized to:
public Task<string> GetElidingKeywordsAsync(string url)
{
HttpClient client;
using (client = new HttpClient())
{
}
return client.GetStringAsync(url);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…