Skip to content

Instantly share code, notes, and snippets.

@matjanos
Created December 14, 2016 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matjanos/74b4661db8cf73507f950ab1fb183b51 to your computer and use it in GitHub Desktop.
Save matjanos/74b4661db8cf73507f950ab1fb183b51 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment