Something like that:
using(WebClient client = new WebClient()) using(Stream stream = client.OpenRead(originGetterURL)) StreamReader reader = new StreamReader(stream) { JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine()); string encryptionKey = (string)jObject["key"]; string originURL = (string)jObject["origin_url"]; }
As for why using blocks are good and better than manually calling Dispose ... image, if any of the code in this using block using exception before you hit the lines where you are closing everything? You are essentially leaking out any unmanaged resource that an IDisposable object manages under the hood. using ensures that Dispose is called correctly, even in the face of an exception (essentially by inserting the appropriate try / finally block).
Whenever possible (i.e. you do not need to maintain the life of any IDisposable by region), you should use using blocks, if not for any other reason, than they reduce the amount of template code that you must write in order so that your own code is safe and correct.
source share