{"id":5073,"date":"2022-06-26T20:16:36","date_gmt":"2022-06-26T18:16:36","guid":{"rendered":"https:\/\/soci.hu\/blog\/?p=5073"},"modified":"2022-06-26T20:16:36","modified_gmt":"2022-06-26T18:16:36","slug":"is-compiling-c-10-code-for-net-4-7-possible","status":"publish","type":"post","link":"https:\/\/soci.hu\/blog\/index.php\/2022\/06\/26\/is-compiling-c-10-code-for-net-4-7-possible\/","title":{"rendered":"Is compiling C# 10 code for .NET 4.7 possible?"},"content":{"rendered":"\n<p>I have asked this question today. Yes, it is possible. Here is the proof:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing System;\nusing System.Linq;\nusing System.Runtime.CompilerServices;\n\n\/\/C# 10\nnamespace ConsoleApp1;\n\ninternal class Program\n{\n    static void Main(string&#x5B;] args)\n    {\n        Console.WriteLine(&quot;Hello C# 10&quot;);\n        CallerArgumentExpressionDemo(args.Length == 0 &amp;&amp; (args.FirstOrDefault() ?? &quot;42&quot;) == &quot;42&quot;);\n\n        int x = 0;\n        \/\/C# 10 - Assignment and declaration in same deconstruction\n        (x, string y) = new DemoStructRecord(2,&quot;apple&quot;);\n        Console.WriteLine($&quot;{x}, {y}&quot;);\n    }\n\n    \/\/C# 10 - CallerArgumentExpression\n    public static void CallerArgumentExpressionDemo(bool b, &#x5B;CallerArgumentExpression(&quot;b&quot;)] string message = null)\n    {\n        if (b)\n        {\n            Console.WriteLine($&quot;Message from the caller expression: {message}&quot;);\n        }\n    }\n}\n\n\/\/C# 10 - record struct\nrecord struct DemoStructRecord(int A, string B)\n{\n    private const string SomeConstant = &quot;Something&quot;;\n\n    \/\/C# 10 - Interpolated string constant\n    private const string InterpolatedConstant = $&quot;This is an interpolated constant {SomeConstant}&quot;;\n\n    public readonly override string ToString()\n    {\n        return $&quot;{nameof(A)}: {A}, {nameof(B)}: {B}&quot;;\n    }\n}\n\nrecord DemoRecord(int A, string B)\n{\n    \/\/C# 10 - sealed ToString\n    public sealed override string ToString()\n    {\n        return $&quot;{nameof(A)}: {A}, {nameof(B)}: {B}&quot;;\n    }\n}\n\npublic readonly struct Measurement\n{\n    \/\/C# 10 - parameterless ctor in struct\n    public Measurement()\n    {\n        Value = double.NaN;\n        Description = &quot;Undefined&quot;;\n    }\n\n    public Measurement(double value, string description)\n    {\n        Value = value;\n        Description = description;\n    }\n\n    \/\/C# 9 (init only props)\n    public double Value { get; init; }\n    public string Description { get; init; }\n\n    public override string ToString() =&gt; $&quot;{Value} ({Description})&quot;;\n}\n<\/pre><\/div>\n\n\n<p>As you see, I packed several C#9 and 10 features into the code, and they compile happily. There were two tricky points, however. CallerArgumentExpression is not defined in .NET Framework; you have to add it to your program:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace System.Runtime.CompilerServices;\n\n&#x5B;AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]\ninternal sealed class CallerArgumentExpressionAttribute : Attribute\n{\n    public CallerArgumentExpressionAttribute(string parameterName)\n    {\n        ParameterName = parameterName;\n    }\n\n    public string ParameterName { get; }\n}\n<\/pre><\/div>\n\n\n<p>The other problematic feature is init-only properties. To be able to compile your code to .NET 4.7, you have to define it yourself:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace System.Runtime.CompilerServices\n{\n    internal static class IsExternalInit { }\n}\n<\/pre><\/div>\n\n\n<p>For completeness, here is the project file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;Project ToolsVersion=&quot;15.0&quot; xmlns=&quot;http:\/\/schemas.microsoft.com\/developer\/msbuild\/2003&quot;&gt;\n  &lt;Import Project=&quot;$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props&quot; Condition=&quot;Exists(&#039;$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props&#039;)&quot; \/&gt;\n  &lt;PropertyGroup&gt;\n    &lt;Configuration Condition=&quot; &#039;$(Configuration)&#039; == &#039;&#039; &quot;&gt;Debug&lt;\/Configuration&gt;\n    &lt;Platform Condition=&quot; &#039;$(Platform)&#039; == &#039;&#039; &quot;&gt;AnyCPU&lt;\/Platform&gt;\n    &lt;LangVersion&gt;10.0&lt;\/LangVersion&gt;\n    &lt;ProjectGuid&gt;{AFFA1FD0-4D9D-4801-B66C-26F232D32B3D}&lt;\/ProjectGuid&gt;\n    &lt;OutputType&gt;Exe&lt;\/OutputType&gt;\n    &lt;RootNamespace&gt;ConsoleApp1&lt;\/RootNamespace&gt;\n    &lt;AssemblyName&gt;ConsoleApp1&lt;\/AssemblyName&gt;\n    &lt;TargetFrameworkVersion&gt;v4.7&lt;\/TargetFrameworkVersion&gt;\n    &lt;FileAlignment&gt;512&lt;\/FileAlignment&gt;\n    &lt;AutoGenerateBindingRedirects&gt;true&lt;\/AutoGenerateBindingRedirects&gt;\n    &lt;Deterministic&gt;true&lt;\/Deterministic&gt;\n  &lt;\/PropertyGroup&gt;\n  &lt;PropertyGroup Condition=&quot; &#039;$(Configuration)|$(Platform)&#039; == &#039;Debug|AnyCPU&#039; &quot;&gt;\n    &lt;PlatformTarget&gt;AnyCPU&lt;\/PlatformTarget&gt;\n    &lt;DebugSymbols&gt;true&lt;\/DebugSymbols&gt;\n    &lt;DebugType&gt;full&lt;\/DebugType&gt;\n    &lt;Optimize&gt;false&lt;\/Optimize&gt;\n    &lt;OutputPath&gt;bin\\Debug\\&lt;\/OutputPath&gt;\n    &lt;DefineConstants&gt;DEBUG;TRACE&lt;\/DefineConstants&gt;\n    &lt;ErrorReport&gt;prompt&lt;\/ErrorReport&gt;\n    &lt;WarningLevel&gt;4&lt;\/WarningLevel&gt;\n  &lt;\/PropertyGroup&gt;\n  &lt;PropertyGroup Condition=&quot; &#039;$(Configuration)|$(Platform)&#039; == &#039;Release|AnyCPU&#039; &quot;&gt;\n    &lt;PlatformTarget&gt;AnyCPU&lt;\/PlatformTarget&gt;\n    &lt;DebugType&gt;pdbonly&lt;\/DebugType&gt;\n    &lt;Optimize&gt;true&lt;\/Optimize&gt;\n    &lt;OutputPath&gt;bin\\Release\\&lt;\/OutputPath&gt;\n    &lt;DefineConstants&gt;TRACE&lt;\/DefineConstants&gt;\n    &lt;ErrorReport&gt;prompt&lt;\/ErrorReport&gt;\n    &lt;WarningLevel&gt;4&lt;\/WarningLevel&gt;\n  &lt;\/PropertyGroup&gt;\n  &lt;ItemGroup&gt;\n    &lt;Reference Include=&quot;System&quot; \/&gt;\n    &lt;Reference Include=&quot;System.Core&quot; \/&gt;\n    &lt;Reference Include=&quot;System.Xml.Linq&quot; \/&gt;\n    &lt;Reference Include=&quot;System.Data.DataSetExtensions&quot; \/&gt;\n    &lt;Reference Include=&quot;Microsoft.CSharp&quot; \/&gt;\n    &lt;Reference Include=&quot;System.Data&quot; \/&gt;\n    &lt;Reference Include=&quot;System.Net.Http&quot; \/&gt;\n    &lt;Reference Include=&quot;System.Xml&quot; \/&gt;\n  &lt;\/ItemGroup&gt;\n  &lt;ItemGroup&gt;\n    &lt;Compile Include=&quot;CallerArgumentExpressionAttribute.cs&quot; \/&gt;\n    &lt;Compile Include=&quot;IsExternalInit.cs&quot; \/&gt;\n    &lt;Compile Include=&quot;Program.cs&quot; \/&gt;\n    &lt;Compile Include=&quot;Properties\\AssemblyInfo.cs&quot; \/&gt;\n  &lt;\/ItemGroup&gt;\n  &lt;Import Project=&quot;$(MSBuildToolsPath)\\Microsoft.CSharp.targets&quot; \/&gt;\n&lt;\/Project&gt;\n<\/pre><\/div>\n\n\n<p>I intentionally used the old project file format for this demo. It is useful to convert older projects to the new SDK format; however, it needs extensive testing, so we cannot schedule it for a while.<\/p>\n\n\n\n<p>(For a nuget package, if the target is an SDK project or the older project with package references, nuget won&#8217;t copy nuget content to the destination project; it only links to the files. This design decision has many severe implications, but I don&#8217;t diverge more in this article.)<\/p>\n\n\n\n<p>Here is the output of the running code:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"761\" height=\"85\" src=\"https:\/\/soci.hu\/blog\/wp-content\/uploads\/2022\/06\/image-14.png\" alt=\"\" class=\"wp-image-5074\"\/><\/figure>\n\n\n\n<p>It&#8217;s interesting to see how the CallerArgumentExpressionAttribute works. It nicely receives the argument C# expression.<\/p>\n\n\n\n<p>To make sure I compiled for .NET 4.7, I decompiled the output assembly by DotPeak:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1204\" height=\"684\" src=\"https:\/\/soci.hu\/blog\/wp-content\/uploads\/2022\/06\/image-15.png\" alt=\"\" class=\"wp-image-5077\" srcset=\"https:\/\/soci.hu\/blog\/wp-content\/uploads\/2022\/06\/image-15.png 1204w, https:\/\/soci.hu\/blog\/wp-content\/uploads\/2022\/06\/image-15-768x436.png 768w\" sizes=\"auto, (max-width: 1204px) 100vw, 1204px\" \/><figcaption>It is compiled for .NET 4.7<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>I have asked this question today. Yes, it is possible. Here is the proof: As you see, I packed several C#9 and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,170,10,4],"tags":[],"class_list":["post-5073","post","type-post","status-publish","format-standard","hentry","category-net","category-dotnet-troubleshooting","category-c","category-szakmai-elet"],"_links":{"self":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/5073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=5073"}],"version-history":[{"count":4,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/5073\/revisions"}],"predecessor-version":[{"id":5142,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/5073\/revisions\/5142"}],"wp:attachment":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=5073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=5073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=5073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}