![]() I recommend reading C# Lowering from community member Steven Giesel, and Lowering in the C# Compiler by Matt Warren. As you saw above in the three examples, lowering can produce a wide range of outputs, so it’s fun to experiment and see what’s happening. Likewise, viewing lowered versions of existing code can help you better understand it and spot potential bugs. So while you could ignore it, using ReSharper’s IL Viewer can help you make more sense of newer C# language features. ![]() Lowering happens to your C# code, whether you know about it or not. NET produces a lowered output of 108 lines! While those lines of code may be difficult to read for us as humans, the compiler has an easier time taking the lowered-form output and turning it into IL. Those features include the var keyword, async/await, and the using statement. Those two lines of C# code use three high-level features of C#, and each gets lowered. Let’s try one more, and this time, let’s go wild with it! using var httpClient = new HttpClient() While the lowered version is much simpler to read and comprehend, the version on the left is much nicer to type. NET has lowered our single-line initialization of a List into five separate calls to Add. List items = new () Ĭonsole.WriteLine(string.Join(",", items)) ![]() Let’s also look at a simple List initialization and what ReSharper’s IL Viewer reveals. NET lowered the foreach implementation into a combination of while and try/catch constructs. As you can see in the following screenshot. Looking at ReSharper’s IL Viewer, we can choose between three viewing states: IL, Low-Level C#, and High-Level C#. Let’s start with the humble foreach loop, likely used by millions of developers across the. Now that you have a general idea of “lowering”, let’s use ReSharper to see some of these high-level features in their lower forms. This process occurs before the compiler begins translating C# into the Intermediate Language (IL) used by the. While you could do this manually, many of these translations automatically happen during the build process. Lowering transforms high-level language features into “simpler” techniques within the same language. ![]() For example, you could easily interpret a foreach block into a lower version of a for block. You can look at these keywords as shorthand for other basic language building blocks. Most modern C# codebases include high-level keywords such as foreach, var, using, async, and await. When you write C# code, your ultimate goal is to compile the code to a runnable artifact, but it’s rarely a simple one-step process. This post will discuss “lowering” and how ReSharper can help you uncover the magic behind the curtain. How do C# language designers keep packing in new language features while allowing you to target older runtimes and keeping your applications performant? Well, it’s all about Lowering. Its high-level features allow you to write terse expressions that typically require several lines of code in lower-level languages such as C. The C# programming language is jam-packed with so much syntactic sugar it would make your dentist mad. ![]()
0 Comments
Leave a Reply. |