TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

C# 9 top-level programs and target-typed expressions

136 pointsby benaadamsabout 4 years ago

16 comments

twodaveabout 4 years ago
I mean, top-level programs are nice, I guess? IMO it&#x27;s more of a means of attracting non-.NET developers into the fold. I work in a .NET shop and we probably won&#x27;t use this in our production code.<p>The benefit of target-typed expressions IMO is that it changes property syntax from:<p>public List&lt;Foo&gt; Foos = new List&lt;Foo&gt;();<p>to simply:<p>public List&lt;Foo&gt; Foos = new();<p>It may seem subtle, but if you write C# you see this pattern constantly.<p>Honestly, these are the features in C#9 I&#x27;m least excited about. I&#x27;m much more excited about record types, init-only setters, not to mention the improved pattern-matching and type inference (no more casting nulls inside of ternary operators!).<p>Immutability in C# has classically been pretty cumbersome. I work on a payroll platform where we&#x27;ve managed to work around a lot of that, but we&#x27;re excited to introduce record types and init-only setters in particular to help with some of the maintenance pain-points surrounding the elimination of side-effects from things like pay calculations.
评论 #26642776 未加载
评论 #26645523 未加载
评论 #26648276 未加载
评论 #26646656 未加载
whoisthemachineabout 4 years ago
To me, the benefit of &quot;Target-typed new expressions&quot; is more with bringing consistency to class-scope member declarations and method-scope member declarations, so these two can look the same, and a programmer only has to read one way of declaring new members in C#:<p><pre><code> public class MyClass { private readonly MyDependency _myDependency = new(); public void MyMethodThatDoesWork() { MyVariable variable = new(); } } </code></pre> As opposed to the (now old, IMO) var syntax&#x2F;Java-like syntax mixture:<p><pre><code> public class MyClass { private readonly MyDependency _myDependency = new MyDependency(); public void MyMethodThatDoesWork() { var variable = new MyVariable(); } } </code></pre> Edited for HN formatting.
评论 #26640701 未加载
评论 #26642450 未加载
outside1234about 4 years ago
Who would have believed a decade ago that there would be a RedHat developer blog entry on C# today?<p>If someone had told you that then you would have thought the world would have collapsed in the meantime.
评论 #26640888 未加载
评论 #26641651 未加载
binarynateabout 4 years ago
I think that C# is underrated. Although TypeScript is my default language choice for most programming, C# &#x2F; .NET is my choice for cases where:<p>- high performance is important - or interop with native libraries is required (because C#&#x27;s DllImport attribute makes that super simple)<p>Another benefit is that C# is syntactically similar to TypeScript (they were both designed by Anders Hejlsberg after all), so switching between the two languages feels easy. Java and Go are both similar to C# in terms of performance, but interfacing with native code isn&#x27;t as simple with those languages, and they&#x27;re also not as similar to TypeScript as C# is (especially Go, which is quite different).
评论 #26640839 未加载
评论 #26642703 未加载
评论 #26641203 未加载
评论 #26641550 未加载
评论 #26641964 未加载
评论 #26641913 未加载
goto11about 4 years ago
The article states that the benefit of target-typed new-expressions is that &quot;the type declarations are nicely aligned&quot; compared the use of &quot;var&quot;.<p>I think a better justification is that fields and properties does not support type inference, so this can avoid some redundant type expressions for initializes, just like &quot;var&quot; can avoid redundant type expressions for local variables.<p>Of course it would be more elegant if fields&#x2F;properties supported type inference like &quot;var&quot;, but that is a can of worms since you can have recursive dependencies.
emodendroketabout 4 years ago
I’m not working with C# anymore but I’m really liking the features they keep adding.
belinderabout 4 years ago
Unfortunately the new tricks can&#x27;t be used when the codebase is still stuck on .net framework :(
评论 #26641157 未加载
评论 #26640998 未加载
评论 #26640863 未加载
评论 #26640643 未加载
评论 #26640567 未加载
aidenn0about 4 years ago
Can anyone comment on cross-platform GUI for .net? Last time I seriously investigated it, Gtk# seemed to be the primary option. Some quick googling found avalonia, but no clue if that&#x27;s a reasonable GUI to use. I&#x27;m guessing MS isn&#x27;t planning on porting WPF to linux (though I suppose linking with wine might work)...
评论 #26643185 未加载
Shadonototroabout 4 years ago
+1 for top-level programs<p>-1 for target-typed expressions, it makes code unreadable, i&#x27;d rather see var used as field members
评论 #26644405 未加载
FridgeSealabout 4 years ago
&gt; Top-level programs allow you to write the main method of your application without having to add a class with a static Main method<p>Riveting stuff happening in .Net land! Sarcasm aside, this seems like such a small, oddly-specific, once-off feature that its like...why bother? Any C# devs want to enlighten me here?
评论 #26642912 未加载
评论 #26643511 未加载
评论 #26644251 未加载
kwertyoowiyopabout 4 years ago
Can I just create a single .cs file somewhere, and execute it by typing its name? No Visual Studio solution&#x2F;project shenanigans? That’s my dream. Top-level programs to eliminate boilerplate is a nice related step.
评论 #26656498 未加载
评论 #26642932 未加载
评论 #26644655 未加载
评论 #26642517 未加载
ryandrakeabout 4 years ago
Wow, a full-screen cookie pop-up with a “X” dismiss button in the upper right corner that just causes the full-screen pop-up to re-display. Can it get more obnoxious?<p>This is the second one on the HN front page today [1]. HN tries to avoid paywalled articles, maybe we should also discourage articles that start out by deliberately hiding the content.<p>1: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=26639722" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=26639722</a>
syspecabout 4 years ago
I really love c# - one of my favrorite languages, but can any one explain the benefit of this new syntax?<p><pre><code> Person p1 = new(); Person p2 = new(&quot;Tom&quot;); Person p3 = new() { FirstName = &quot;Tom&quot; };</code></pre>
评论 #26656249 未加载
评论 #26644305 未加载
lloydatkinsonabout 4 years ago
I&#x27;m very cautious of anything from RedHat regarding .NET - some of the worst C# I&#x27;ve ever seen comes from some of the devs working at RedHat in the form of commits to public repositories.
joshsynabout 4 years ago
Scala 3 kills C# by a large margin, imho. C# is not a good pursuit unless you are stuck with .Net codebase.
评论 #26642898 未加载
评论 #26642869 未加载
评论 #26644331 未加载
danielovichdkabout 4 years ago
I have been programming with c# since 2000 i think. But I do not like where the languages is going.<p>It is becoming way to loose in a sense and it seems that it wants more than it should.<p>If you want to do functional programming, pick a fp language.<p>If you want to do dynamic typed programming pick a dp language.<p>Sure you can mix some things in, but c# is becoming way to scattered imo. And it&#x27;s not pretty i think.
评论 #26641008 未加载
评论 #26641151 未加载
评论 #26641951 未加载
评论 #26641287 未加载
评论 #26641755 未加载
评论 #26641299 未加载