Skip to content

Instantly share code, notes, and snippets.

@matjanos matjanos/Program.cs

Created Dec 14, 2016
Embed
What would you like to do?
DevStyle - konkurs
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello DevStyle!");
FileStream textStream = File.Open("test.txt",FileMode.Open);
try
{
StreamReader reader = new StreamReader(textStream);
Console.Write(reader.ReadLine());
}
finally
{
if (textStream != null)
((IDisposable)textStream).Dispose();
}
}
}
@matjanos

This comment has been minimized.

Copy link
Owner Author

matjanos commented Dec 14, 2016

This file is decompiled to:

using System;
using System.IO;

internal class Program
{
  private static void Main(string[] args)
  {
    Console.WriteLine("Hello DevStyle!");
    using (FileStream fileStream = File.Open("test.txt", (FileMode) 3))
      Console.Write(new StreamReader((Stream) fileStream).ReadLine());
  }
}

Of course using(...) statement is an example of syntactic sugar, so compiler changes it into the the code that consist of more basic instructions that are realy implemented in .NET Framework.
The dotPeek that I used in order to archive this effect is aware of those syntatic sugars and assumes that developers use them. Compiled IL looks the same in case of using and the try{} finally{} statements, so the result of decompiling simplify it to nicer using.

@matjanos

This comment has been minimized.

Copy link
Owner Author

matjanos commented Dec 14, 2016

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.