TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

136 点作者 benaadams大约 4 年前

16 条评论

twodave大约 4 年前
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 未加载
whoisthemachine大约 4 年前
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 未加载
outside1234大约 4 年前
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 未加载
binarynate大约 4 年前
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 未加载
goto11大约 4 年前
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.
emodendroket大约 4 年前
I’m not working with C# anymore but I’m really liking the features they keep adding.
belinder大约 4 年前
Unfortunately the new tricks can&#x27;t be used when the codebase is still stuck on .net framework :(
评论 #26641157 未加载
评论 #26640998 未加载
评论 #26640863 未加载
评论 #26640643 未加载
评论 #26640567 未加载
aidenn0大约 4 年前
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 未加载
Shadonototro大约 4 年前
+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 未加载
FridgeSeal大约 4 年前
&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 未加载
kwertyoowiyop大约 4 年前
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 未加载
ryandrake大约 4 年前
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>
syspec大约 4 年前
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 未加载
lloydatkinson大约 4 年前
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.
joshsyn大约 4 年前
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 未加载
danielovichdk大约 4 年前
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 未加载