1

When @No One was answering my question here he had mentioned something like below,

 using(SomeClass c = new SomeClass())
 { }

**

will translate to

**

 try
 {
     SomeClass c = new SomeClass();
 }
 finally
 {
   c.Dispose();
 }

Well, how do you guyz tell that?

Also, some of you mentions that "internally it will be implemented in a way" , how do you see the internal implementation of something?

For example, while I was browsing through "concepts of generics" in MSDN, they say that List is generic and showed a sample implementation of how list is implemented internally here. How do we really get that for others?

I tried in VS 2010. But the object browser shows only the methods or fields exposed of a particular type.

(I am just being curious because the example of the internal implementation of list is just awesome. really interesting stuff!)

Community
  • 1
  • 1
  • If you are interested in how some of the framework classes are actually implemented, Microsoft has made the source code available [here](http://referencesource.microsoft.com/netframework.aspx). – Mike Zboray Sep 05 '13 at 06:53

3 Answers3

3

C# specification explains this in chapter 8.13:

A using statement of the form

using (ResourceType resource = expression) statement

corresponds to one of three possible expansions. When ResourceType is a non-nullable value type, the expansion is

{
  ResourceType resource = expression;
  try {
      statement;
  }
  finally {
      ((IDisposable)resource).Dispose();
  }
}

Otherwise, when ResourceType is a nullable value type or a reference type other than dynamic, the expansion is

{
  ResourceType resource = expression;
  try {
      statement;
  }
  finally {
      if (resource != null) ((IDisposable)resource).Dispose();
  }
}

Otherwise, when ResourceType is dynamic, the expansion is

{
  ResourceType resource = expression;
  IDisposable d = (IDisposable)resource;
  try {
      statement;
  }
  finally {
      if (d != null) d.Dispose();
  }
}

And about your second question: .NET source code is available to browser/download. Check this question: Downloadable/browsable version of the .NET Framework source code?

Community
  • 1
  • 1
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
3

Marcin has nailed it for the specific transformation you're talking about here, with the using statement. However, you should also be aware of tools like Reflector and dotPeek (there are many more) as well as ildasm.

Reflector and dotPeek will decompile your code back from the compiled code to C#, with various levels of "optimization" (how hard it tries to do the C# compiler's transformation, basically) and they also allow you to see the IL - which is what ildasm gives you too.

The IL gives you the ultimate answer about what the compiled code looks like, but often looking at the decompiled code (particularly with "optimizations" turned off, or setting the decompiler to provide C# 2 code for example) can be simpler.

It's worth being aware that many of the transformations performed by the compiler will use "unspeakable names" for extra types, fields and methods that are generated. For example if you have:

Action foo = () => Console.WriteLine("I'm a lambda!");

then that may generate a method called something like <Main>b__0. You wouldn't be able to refer to this in normal C# (as a C# identifier can't include < or >) but it's valid IL.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

It sounds like you are looking for a decompiler. There are several to choose from, but a popular free one is ILSpy. There is also dotPeek which is free. If you want more features and don't mind spending money, you might want to look at .Net Reflector.

Dan
  • 9,717
  • 4
  • 47
  • 65
  • I used to use Reflector when it was free, but I've been using ILSpy since Reflector started charging money. Some of my colleges like dotPeek, but I haven't used it. – Dan Sep 05 '13 at 06:46