4

So this is a pretty simple question, but I don't see an answer to it anywhere.

If I somehow generate CIL code manually, is it possible to do things that would be prohibited in C#? What kind of things? (Obviously I'm not expecting an exhaustive list.)

I haven't looked into the fine details of CIL very far. But given that the CLR is supposed to be language-agnostic yet support tight coupling between components written in different languages, I would expect CIL to work at the object-level and perhaps have its own type system. I can imagine that type system being less strict than C#, and possibly a few other things that could be different. But I was wondering if anybody knows definitively without having to read the entire language specification...

MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
  • 1
    The [CLI Specification (ECMA-335)](http://www.ecma-international.org/publications/standards/Ecma-335.htm) is available. It's pretty big (and requires digging), but it does explicitly call out a few issues between languages - and exposes the building bricks of what 'can' be done. – user2864740 Mar 17 '15 at 19:28
  • 2
    Simple answer is yes. But probably not anything you'd particularly want to do. If you look through the issues on the Roslyn github repo specific examples come up now and again. – James Gaunt Mar 17 '15 at 19:29
  • 2
    Sure, C# doesn't support module constructors for example. Or the raise accessor for events. Or constraints on enums and delegates. Or user-defined value types. Enough? – Hans Passant Mar 17 '15 at 19:29
  • 1
    Yes. For a start, you can express generic type constraints on `System.Enum` and `System.Delegate`... – Jon Skeet Mar 17 '15 at 19:29
  • 3
    Given that there are things that can be done in .NET languages that aren't C# and not in C#, obviously things can be done in MSIL and not in C#. Just search out any "how are VB/F#/J#/etc. and C# different" to look for examples. – Servy Mar 17 '15 at 19:30
  • 1
    @Servy that logic is a bit flawed. Languages can expose things that are enabled by the compiler itself rather than the underlying IL. For example, you could say VB has XML literals and C# does not, but in fact this is not a feature of IL. Many recent compiler feature are not IL related. E.g. in C# iterators (`yield`) or `await/async` or even LINQ are purely syntactical sugar that uses no special MSIL feature. – jods Mar 18 '15 at 18:56

1 Answers1

2

Sure. Nothing that enables you to achieve tasks impossible to code in C# (Turing-completeness and so on) but there are programming constructs not currently exposed in C#. Note that the upcoming C# 6 adds some of those.

A certainly not complete list, off the top of my head:

  • try .. fault blocks. They execute only when the try block is exited on an exception.
  • Exception filters. VB already has them and C# 6 will have them as well: catch .. when.
  • Struct (value types) parameterless constructors. C# 6 will have them. EDIT: svick commented that the feature was dropped.
  • Generic type constraint on enum.
  • A protected and internal visibility modifier (you have to be a derived class in the same assembly to have access to the field).

I'm sure there are more.

jods
  • 4,581
  • 16
  • 20
  • You ment private protected? http://stackoverflow.com/a/22857053/1277156 Edit: Nvm, you said "protected AND internal" which is what it does but was expecting the name. – Measurity Mar 17 '15 at 20:16
  • [Struct parameterless constructors won't be in C# 6.0 after all.](https://github.com/dotnet/roslyn/issues/1029) – svick Mar 18 '15 at 15:05
  • @Measuring yes exactly. The feature was proposed for C# 6 under the name `private protected` but there was a huge debate about the name and the C# team decided to postpone the feature after no consensus on a good name was found. – jods Mar 18 '15 at 18:49