7 ms·
Ask HN: Examples of Top C# Code?
I want to look at some "best" C# code (whatever you think best or top code is).
Do you know public C# code that you would recommend to look at it because it is well designed or very well readable?
- stevefan1999 4y agohttps://stackoverflow.com/a/4488073 https://stackoverflow.com/a/4488073
- w4ffl35 4y agoFine tuning an AI model?
- ycuser2 4y agoNo, just curious and want to improve my coding style.
- dajo 4y agoI like the Stripe API design, and as a follow-on, I tend to like their coding styles and structures. Here's their official Stripe dotnet library - https://github.com/stripe/stripe-dotnet https://github.com/stripe/stripe-dotnet. It's well organized and the code coverage looks good.
- smithcoin 4y agoThis applies to their libraries in other languages as well, their Ruby one is a great reference too.
- automatic6131 4y agoDoes this, errr, do anything? If it's just a C# adapter for a web api, then it's extremely dull infrastructure code. All you get is code style opinions and folder choices.
- DoctorOW 4y agoCode style opinions and folder choices is vaguely what OP is asking for though, right?
- Hawxy 4y agoI'd argue this library design is fairly ancient in C# land. The reliance on static configuration values goes against modern DI-centric design + integration patterns. It also doesn't have a central entry-point for discoverability and instead expects each internal client to be used directly.
- tren 4y agoI like Bitwarden (https://github.com/bitwarden https://github.com/bitwarden), it's a great app
- bevacqua 4y agoI learned C# with notepad and RunUO as a teenager. Extremely good codebase https://github.com/runuo/runuo https://github.com/runuo/runuo
- can16358p 4y agoSame here. As an UO addict, after messing with SphereServer and moving to RunUO, it was my first sight of C# code. Then, it came a looooooong way to here today.
- philliphaydon 4y agoMight enjoy taking a look at Classic UO. It's UO, written in .NET, to run in the browser... https://github.com/ClassicUO/ClassicUO https://github.com/ClassicUO/ClassicUO https://www.classicuo.org/ https://www.classicuo.org/
- pancholopez 4y agohttps://github.com/DuendeSoftware/IdentityServer https://github.com/DuendeSoftware/IdentityServer https://github.com/microsoft/calculator https://github.com/microsoft/calculator if you go on github you can browse C# repositories, there are good examples there https://github.com/topics/csharp https://github.com/topics/csharp
- kryptiskt 4y agoIt's an absolute massive codebase, but the compilers and analyzers in Roslyn[0] are very clearly written. For a random example, here is the analyzer finding variable swaps of the form tmp = a; a = b; b = tmp; that can better be done using tuples: https://github.com/dotnet/roslyn/blob/main/src/Analyzers/CSharp/Analyzers/UseTupleSwap/CSharpUseTupleSwapDiagnosticAnalyzer.cs https://github.com/dotnet/roslyn/blob/main/src/Analyzers/CSh... And here is the code fix that automatically performs the substitution: https://github.com/dotnet/roslyn/blob/main/src/Analyzers/CSharp/CodeFixes/UseTupleSwap/CSharpUseTupleSwapCodeFixProvider.cs https://github.com/dotnet/roslyn/blob/main/src/Analyzers/CSh... [0]: https://github.com/dotnet/roslyn https://github.com/dotnet/roslyn
- _Chief 4y agoA tip for going over github code is to use the github.dev domain. This will help going over the code in an IDE (if you'd like that), that provides you language features, and easier navigation (eg Go To/ Peek Definitions, or References). It also helps avoid to clone a large repo to do the same locally. For example https://github.dev/dotnet/roslyn/blob/main/src/Analyzers/CSharp/Analyzers/UseTupleSwap/CSharpUseTupleSwapDiagnosticAnalyzer.cs https://github.dev/dotnet/roslyn/blob/main/src/Analyzers/CSh... (takes a few mins to cache such a large repo, but caches in browser local storage for subsequent use)
- bob1029 4y agoPressing '.' while in the code area of GitHub.com will automagically take you to dev space.
- ystad 4y agoThe .NET source https://github.com/microsoft/referencesource https://github.com/microsoft/referencesource https://github.com/microsoft/dotnet https://github.com/microsoft/dotnet Unity reference source https://github.com/Unity-Technologies/UnityCsReference https://github.com/Unity-Technologies/UnityCsReference The above are example of production code. I use these as references all the time Awesome c# https://github.com/quozd/awesome-dotnet https://github.com/quozd/awesome-dotnet
- runevault 4y agoI'd be careful about recommending Awesome dotnet, their list has some deprecated codebases like NancyFx (I'd bet others but I know that one)
- dubiousconst281 4y agoReferenceSource is the code from the Windows framework and I'm pretty sure it should be considered deprecated at this point. The runtime and core libraries are now at https://github.com/dotnet/runtime https://github.com/dotnet/runtime, kinda weird that this is not directly linked in the home page repo.
- paoda 4y agoIn my personal opinion, the rhythm game osu! [1] and the related osu!framework [2] are great examples of C# code. [1] https://github.com/ppy/osu https://github.com/ppy/osu [2] https://github.com/ppy/osu-framework https://github.com/ppy/osu-framework
- Lapsa 4y agooh yeah... peppy's a beast
- fercircularbuf 4y agoShameless plug for a site I started aimed at increasing visibility of high quality source code for language learners: http://goodsourcecode.org http://goodsourcecode.org It is a proof of concept, and I'm always looking for people to contribute!
- flanbiscuit 4y agoI like the concept and hope to see this expanded. I have a suggestion: Could you maybe add some sort of tag cloud, or list, of the current languages in the database? I typed in Javascript, Go, then Python, and no results. Seems like you only have C and PHP in your database at the moment. Listing all of the languages covered by this database would be helpful. thanks!
- Sheeny96 4y agoNot a .NET developer, but even as such, the Bitwarden code was very readable to me https://github.com/bitwarden/server https://github.com/bitwarden/server
- jve 4y agoWait, is my favorite password manager written in my favorite language? Goodies.
- danielbarla 4y agoInteresting, I didn't know it was C#. One thing that immediately poked my eye is that it's also using MS SQL Server, and I am wondering why it'd be picked over various open source alternatives. My main guess is 'familiarity of the developers', but is there some MS SQL Server capability that is being used that is advantageous for the project? A cursory scan of the code makes it look like fairly simple SQL that would be perfectly at home in just about any RDBMS, and most of the others wouldn't come with the rather high licensing costs. Don't get me wrong, I think MS SQL Server is a highly capable DB, I just don't see enough of a value add here to justify the price tag, in general.
- chokolad 4y ago> Don't get me wrong, I think MS SQL Server is a highly capable DB, I just don't see enough of a value add here to justify the price tag, in general. I suspect it uses what was called MSDE (MS SQL desktop engine), which is free to use and redistribute on Windows. It is essentially headless SQL Server with most high-end features stripped out and resource governor enabled. With all those limitations it is still very capable (probably too capable for the use case) database.
- danielbarla 4y agoYes, I suspect you are right! In these types of desktop, single-user use cases I would default to SQLite.
- beart 4y agoThis repository is a good place to start with modern c# code, specifically ASP.NET https://github.com/ardalis/CleanArchitecture https://github.com/ardalis/CleanArchitecture
- philliphaydon 4y agoOpened the link, went straight to infrastructure > Data... generic base repository, for EF... Don't think this is a good example.
- caleblloyd 4y agoI have had the pleasure of contributing to a couple different networked drivers with very talented maintainers that I like to use as references. One supports a wide array of Framework versions and has both Sync and Async I/O, as it must to implement the ADO.NET database driver interfaces. Reading the internals really highlight the way that .NET has evolved over the years and what must be done in each target version to maximize performance: https://github.com/mysql-net/MySqlConnector https://github.com/mysql-net/MySqlConnector The other supports .NET 6 only with Async I/O only. This support policy seems to be the way that "modern" .NET development is headed, as .NET 6 will be the floor for LTS .NET (formerly .NET Core) releases in a few months. Async APIs only greatly simplify development, and make it simpler to remain performant when targeting WASM. https://github.com/Cysharp/AlterNats https://github.com/Cysharp/AlterNats As a library maintainer, one thing I often wonder about is how to indicate .NET version support. One option would be for the major version of the library to track the major version of .NET, so if I were to publish a new library today then start with .NET 6 support and start with version number 6.0.0 instead of 1.0.0. This would limit the library to only making breaking changes when the .NET version changes though.
- TillE 4y agoUnless you're being paid by customers with specific needs, or targeting some weird platform, my expectation would be that libraries don't support old .NET versions an instant longer than Microsoft does. So .NET 6 support can be dropped in late 2024, and I don't think that kind of thing even requires a major version bump. The old framework is dead.
- mastax 4y agoI was looking at the CSV parser Cursively recently, and I think it is a good simple example of a high performance C# parser and API design. https://github.com/airbreather/Cursively https://github.com/airbreather/Cursively
- francisofascii 4y agoJust to add to the question. I suspect many developers (and perhaps especially C#) are developing CRUD apps. Are there any public repos that are CRUD apps? It seems like many public C# repos are things like libraries, frameworks, etc.
- feoren 4y agoCRUD apps don't really get open-sourced for several reasons: (1) CRUD apps tend to be commercial or internal -- most people don't build CRUD apps for fun -- so they're heavily IP-protected. (2) CRUD apps tend to be highly specific to the way a particular company or department does something, and then require lots of customization on top of that, and then usually don't do their job very well anyway. You wouldn't be able to just grab the code and database schema from the internal support department of some company and use it in your own as-is. (3) People haven't figured out how to make them highly reusable, such that you could pull a library from NuGet and add a full column of CRUD functionality to another CRUD app. (They haven't figured it out because they're trained in awful, simplistic database practices and design inflexible, fragile, uncuttable, single-use databases, and then design their entire application as a simple layer around that database. And yes, I'm talking about you, dear reader. It's actually astonishing how backward the current standard of database design is.) That all being said, an actually well-designed, fully fleshed-out CRUD app would be very useful as a reference. Tutorials just never even scratch the surface.
- binarray2000 4y agoRe (3): Such comments always tickle my curiosity and my thirst for knowledge. So, which books, courses, codebases do you recommend for both beginners and us with beginner's mind that would help us design and build databases which are the opposite of what you describe?
- mtone 4y agoBoth bigger than bog-standard CRUD apps, but there is: https://github.com/dotnet-architecture/eShopOnWeb https://github.com/dotnet-architecture/eShopOnWeb - Demo app from MS https://github.com/jellyfin https://github.com/jellyfin - A plex alternative
- CyanLite2 4y agoFollow @davidfowl on Twitter/Github and look at "Minimal API" code for beautiful C# code and examples for specific scenarios. C# is VERY different from even just what things were 2 years ago. You can now build a full-on app with the same number of lines that a Node/Express/Flask app. Except the .NET runtime is about 5-10x faster than those runtimes.
- quickthrower2 4y agoI am a .NET dev not been keeping up admittedly but was blown away by the Blazor docs and what that can now do. HN comments are what made me look. Last time I looked it was a POC. I didn’t realize it had become a thing! It might convince me not to bother learning Phoenix.
- c7DJTLrn 4y ago>Except the .NET runtime is about 5-10x faster than those runtimes. I'm as guilty as anyone for using anecdotal evidence but any reliable numbers that prove this? Stats I recall from years ago showed V8 destroying the .NET CLR.
- SideburnsOfDoom 4y agohttps://www.techempower.com/benchmarks/ https://www.techempower.com/benchmarks/
- olvy0 4y agoSpecifically, comparing Responses per second between C# and javascript web frameworks: https://www.techempower.com/benchmarks/#section=data-r21&test=query&l=zik0sd-6bj https://www.techempower.com/benchmarks/#section=data-r21&tes... One JS framework smokes, but most of the C# ones are above the JS ones. I don't know what's that "just-js" thing at the top though, I suspect it's not really a framework.
- nkozyra 4y agoPedantically, it's a framework but a very targeted one with its own runtime: https://github.com/just-js/just https://github.com/just-js/just
- gavinray 4y ago"DotNEXT" is a repo that enhances the code from .NET core, and has examples of using new API's that you can't even find tutorials for on the internet/examples from in public code. For instance, there's been a class since .NET 6, "RandomAccess", for high-performance random-access file I/O (potentially async), and I couldn't find a single damn use of it on the internet. But then this repo had a whole utility class for it, and it's chock full of similar things: https://github.com/dotnet/dotNext/blob/d4111528297ff3b6567b90e2295df0ac5c4b0e9f/src/DotNext.IO/IO/FileReader.cs#L135-L167 https://github.com/dotnet/dotNext/blob/d4111528297ff3b6567b9... Similarly, I would recommend stuff by the .NET core team. I'm particular biased towards the low-level/interop team. Anything by Tanner Gooding is great, stuff by Michal Strehovský, Aaron Robinson, Elinor Fung. Also PathogenDavid, that guy is a wizard https://github.com/PathogenDavid https://github.com/PathogenDavid
- alkonaut 4y agoRoslyn is the famous example of a well-maintained C# codebase. But I think it's perhaps too complex to serve as an example. It's hard to get a good overview of exactly what it does. For some at least slightly smaller and less intimidating codebases, try for example https://github.com/nodatime/nodatime/ https://github.com/nodatime/nodatime/ https://github.com/DapperLib/Dapper https://github.com/DapperLib/Dapper
- olvy0 4y agoSolutions to Advent of Code problems that people publish on github tend to be fairly short but readable, and most show off the language. This is not "real" production C# code. Real production code would have a lot more checks for edge conditions and error handling that would obfuscate the meaning. But it's good in order to get a feel for the capabilities of the language and its ecosystem, imo. There are a couple of repos you can browse, I recommend this one, but only if you have some basic knowledge of LINQ and/or functional programming in general, else this might scare you off. But even if you don't, you could try to figure out how the solution to the first couple of problems in each year work, and learn through that. This guy tries to use the newest features of the language so it can confusing at time. He includes the full text of the problems in the repo so you can try to understand what's he's trying to do. https://github.com/encse/adventofcode https://github.com/encse/adventofcode Also solving AOC with C# has its own github topic: https://github.com/topics/advent-of-code-csharp https://github.com/topics/advent-of-code-csharp
- jimbobs 4y agoNancyfx - sadly dead in the water now but well worth a read of both the code and docs https://github.com/NancyFx/Nancy/tree/master/ https://github.com/NancyFx/Nancy/tree/master/