8 ms·
C# 11 Preview Updates – Raw string literals, UTF-8 and more
- oaiey 4y agoI really like the removal of the !! operator from being included in the language. The syntax was awkward and the introduction of branching/exceptions into parameter spec felt wrong.
- bob1029 4y ago> The feedback and the wide range of insight we gained from this feedback led us to reconsider this as a C# 11 feature. We do not have sufficient confidence that this is the right feature design for C# and are removing it from C# 11. We may return to this area again at a later date. But Microsoft bad, right?
- oaiey 4y agoYeah they tried for like 2-3 releases but I think they are not happy with adding it. Language design is brutal: you make a mistake which you can never revert. And this group is mature enough to realize it.
- philliphaydon 4y agoLike DateTime. We are stuck with it.
- briHass 4y agoAt least they finally added DateOnly and TimeOnly structs, though support for those is lacking for RDBMS conversions. DT, Offset, and the TZInfo helpers, is better than many languages get. It's braindead simple to convert UTC to a specific TZ and to do date arithmetic.
- ycuser2 4y agoWhat's wrong with DateTime? Seriously curious
- Mindless2112 4y agoIt should have been two types (UtcDateTime and LocalDateTime), and Unspecified should never have been a thing.
- Smaug123 4y agoI have spent weeks of work time trying to track down DateTimes, work out their intended semantics, replace them with a user-defined type, and fix the resulting fallout when it turned out that people were relying on bugs. Unclear who thought it would be a good idea to have the same type mean all of "I don't know whether I have time zone relevance", "I am in UTC", and "I am in local time" depending on some runtime enum it contains.
- Const-me 4y agoFile systems need backward compatibility. 40 years ago, it wasn’t clear people gonna invent Internet and start moving files across time zones. Keeping file modification time according to local clock was reasonable idea at that time. The FAT file system (1977) stores local time, and no time zones. CDFS a.k.a. ISO 9660 (designed in 1986) was better than FAT but still less than ideal, it keeps local time, and offset in 15 minutes intervals between local time and GMT. That’s not generally convertible to UTC either. Windows NT came with the proper file system which keeps time in UTC (also high-resolution, 100 nanoseconds versus 2 second in FAT) but most people only upgraded to NT and NTFS in 2002 with WinXP, and various external drives were still using older file systems without UTC timestamps. .NET only dropped the support of Windows 9.x in .NET 3.0 (2006), and even afterwards they probably wanted to support all these external drives. Including timestamps of the files on these drives.
- Smaug123 4y agoI guess I'm just more willing to accept solutions of the form "recognise the mistake we made, and create a whole new API that is more correct" than they were.
- pelagicAustral 4y agoI just turn my head to look at my bookshelf, and sure enough, there it is, my C# 6.0 & .NET Framework 4.6 book. That was the last version I worked. Nice to read about all the changes in C#, I've been thinking a lot about going back to .NET, but at this point I would probably need some training. There are things like Blazor or .NET Core that I'm absolutely unfamiliar with. I kind of miss Visual Studio for some reason, after what feels like a lifetime of web development in other open source tech stacks.
- philliphaydon 4y agoI think .net 3.1-6 has settled enough now that the confusion is more or less gone.
- oaiey 4y agoOh indeed. The language and the normal coding style evolved significantly, the base class library evolved and the whole base promise of the language switched from supporting UI apps and Windows services to be a top contender for any program type you currently can imagine. Learn .NET Core (nowadays called just .NET). It is a fresh breeze to your memory. So easy, so rewarding compared to the complexity of Java or Node dependency trees.
- jsiepkes 4y ago> So easy, so rewarding compared to the complexity of Java or Node dependency trees. How do Java dependencies differ much from C#?
- oaiey 4y agoI appreciate the inclusion of raw strings. Together with the work on interpolated strings api in previous language / base class library, there might be some nice innovations in the templating corner coming to C#.
- thrower123 4y agoDid I miss something in the Framework -> Core transition where strings aren't UTF-16 internally anymore?
- juki 4y agoNo, they're still UTF-16.
- masklinn 4y agoNo, the internal storage is still UTF-16. What the "UTF-8 literals" feature is really about the conversion of strings to byte[] (and similar). And more specifically the static initialisation of byte[]: initialising a byte[] from a string literal will store the UTF8 encoding of the literal as a bytes array and can be performed from static data (a bytes constant in the binary), before that you had to write the "new byte[]" with individual integral byte value by hand e.g. byte[] thing = new byte[] { 0x77, 0x6f, 0x72, 0x6c, 0x64 }; can now be written byte[] thing = "world";
- bzxcvbn 4y agoObjects of type `string` are internally encoded as UTF-16. Byte arrays or spans can contain anything, including strings encoded as UTF-8 (or whatever encoding you like, or raw binary files, or random bytes). The new feature does exactly what's written in the blog post: if a string literal contains only UTF-8 characters and you assign it to a byte array or span, it gets encoded as UTF-8. It's just syntactical sugar. This is a post about C#11 the language, not the framework or the runtime. It's only telling you about what the compiler does when it encounters some syntax. Under the hood, it probably calls some encoders from the standard library.
- tialaramex 4y ago> if a string literal contains only UTF-8 characters and you assign it to a byte array or span, it gets encoded as UTF-8. I write a bunch of C# for my job, but am far from an expert in the language. My reading of this statement is redundant, which means I feel sure it's trying to communicate something the authors thought was "obvious" and is not. * A string literal - so, realistically some Unicode text, right? All the other encodings anybody was actually using can transliterate to Unicode, so, they are just Unicode (with a different encoding) * contains only UTF-8 characters - UTF-8 is an encoding of Unicode, so, this just means Unicode again I'm guessing actually C# can write something that's not Unicode in a String for some reason? But what that might be is unexplained: Can you... emit arbitrary bytes? But how when your native encoding (UTF-16) isn't even byte oriented? What does that mean? Maybe you can emit the rare Unicode "non-characters" like U+FFFF ? But, you can express those just fine in UTF-8 so who cares? Or perhaps it's as simple as C# lets you write literals which are sequences of 16-bit code units but aren't UTF-16 ?
- thatisit 4y agoAwesome. Our company is considering moving scientific applications from Python to .NET (C# or F#), mostly for stability reasons and better backwards compatibility. We need to wrap C libraries though. Does anyone have experience with or reliable benchmarks for the C interface?
- meibo 4y agoI can't give you any benchmarks, but in my experience (from wrapping a big C++ game engine in generated C#), modern .NET interop overhead is basically unnoticeable. .NET 7 finally also ships with NativeAOT which compiles your C# to native code that you can expose as a C library.
- littlecranky67 4y agoThe Kestrel HTTP server included in ASP.NET was using libuv (async event loop implemention used also in nodejs) for a while (it is removed now and replaced with the managed `Socket` implementation. Since libuv is native code, they had a lot of PInvokes/DllImport in their glue code. The performance was way ahead of what they had in their IIS implementations, so I think you will be good performance wise.
- Const-me 4y agoIt’s pretty fast. Likely reason for that, MS designed both language and runtime this way since version 1.0. They needed that for their Windows Forms which consumes huge chunk of WinAPI. I benchmarked a while ago when testing this library https://github.com/Const-me/ComLightInterop#performance https://github.com/Const-me/ComLightInterop#performance On the computer I was using at that time (probably Ryzen 5 3600 CPU) the overhead was 15-20 nanoseconds per call.
- mikece 4y agoI have been working with .NET since the heady, early days when the web framework was being called ASP-NG (next gen) and C# had not yet been given it's official name. I've written millions of lines of C# but sometimes I think it would behoove me to start over with C# as though I had never seen it before because there are things that have been added to the language in the last few years that I had no clue were there. Is there a course or book or blog post series along the lines of "C# for people who are writing C# like it's 2010"?
- apetrovic 4y agoI was in a similar position, worked with C# from the beginning and recently switched to non-MS stacks. I found official documentation [1] easy enough to follow - I started with "What's new in C# 8.0" to the latest version. [1] https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csh...
- Kinrany 4y agoI'd start with just learning about all the new features by reading the updates in reverse chronological order.
- elorant 4y agoJohn Skeet's "C# In Depth" series presents how the language changes from one version to the next with code examples.
- sandreas 4y agoI recently wrote a little cross platform command line app to tag my audio files [1] with C# (dotnet 6) and I must say: This language is awesome on macOS, Linux AND windows. I personally use Rider as IDE and it feels pretty natural. The new features will make my C# life even easier. [1] https://github.com/sandreas/tone https://github.com/sandreas/tone
- wpdev_63 4y agoFound out the other day the nintendo switch emulator ryujinx is written in C#. It's the first I've heard of a performant emulator written with a gc language and it's amazing. It's as fast as its competitor written C++ which goes to show that C# can be just as fast as language without a runtime with minimal gc pauses.
- mastax 4y agoC# has a lot of performance oreinted features that are rare to find in GC languages. Value types and real Generics, of course, which helped during the dark years when the JVM was clearly better. There's relatively efficient and convenient interop with unmanaged function pointers and other helpers. Ref structs like Span make it easy to efficiently and safely pass around references to buffers rather than copies. In, out, and ref parameters and readonly structs help reduce unnecessary copies in function arguments. The .NET team has been optimizing the standard library using C# SIMD intrinsics. It's a great language for writing convenient code most of the time, and hyper optimizing where it counts.
- gavinray 4y agoHaving done a fair bit of C/C++ interop stuff with C#, I would say this is definitely its standout compared to Java. The things you mention are huge, along with "UnmanagedCallersOnly", the ability to write structs and mark them as meant to be used in interop code (see "Blittable/Unmanaged Types" proposal that was recently adopted), and the seamless "DllInterop" annotations for pulling in native external methods. https://devblogs.microsoft.com/dotnet/improvements-in-native-code-interop-in-net-5-0/ https://devblogs.microsoft.com/dotnet/improvements-in-native... https://github.com/jkoritzinsky/runtime/blob/a5048c2fe5068e94e78f46cdc6e915c90039368f/docs/design/libraries/DllImportGenerator/StructMarshalling.md https://github.com/jkoritzinsky/runtime/blob/a5048c2fe5068e9... The team that works on the native/interop stuff is top-notch, and they're constantly pushing boundaries and making neat improvements. I'm more of a JVM guy, but credit is well due here.
- daniel-thompson 4y agoNot familiar at all with this codebase, but one thing to note is that it does make heavy use of the unsafe keyword^. https://github.com/Ryujinx/Ryujinx/search?q=unsafe https://github.com/Ryujinx/Ryujinx/search?q=unsafe
- yyyk 4y agoI like C#, but the language authors have a tendency to overcomplicate with unintuitive semantics like significant spaces at the closing of the raw string literal (otherwise a very welcome feature) or struct vs record struct etc.
- Metasyntactic 4y agoHey there! Language designer of Raw Strings here. The use of whitespace to affect the meaning of strings was actually a big ask from the community and was due to a lot of confusion and frustration over the years from customers who didn't like that if they had a multiline string that it would then not indent properly with all the rest of their code and would cause things to look cluttered and unpleasant. Users participated in the design here and we got feedback from thousands in multiple venues about this design. The behavior around whitespace here was viewed as being both very beneficial and intuitive. Finally, to help out here, the VS ide will also draw a line showing what part of the code is content and what is not. You feel this is overcomplicated, but this was the result of a lot of cooperative design with a lot of the user base to something that was near universally felt to be desirable over not having this behavior. :-)
- yyyk 4y agoThanks for Raw Strings! That was one of my annoyances and now it will be much easier. >Finally, to help out here, the VS ide will also draw a line showing what part of the code is content and what is not. This will help a lot, especially people who don't expect this behaviour. Practically answers my problem with this (when using VS at least). Still not my preferred solution, but I guess I got outvoted.
- yesiknowitbettr 4y agoUseless features. What C# really needs is the following: - Discriminated unions - Switch expressions as standalone statements - Type providers, so it will verify the content of the text, provide syntax highlight, autocomplete, etc for many languages and data structures: var sql = <SQL>""" SELECT Id, Name FROM Users """ - Pipe operator to avoid nested method calls (similar to F#)
- deleted 4y ago[deleted]
- jnash 4y agoWhy oh why does C# still not have sum types? It is by far the most useful feature still missing from C# and one of the key reasons why I prefer F# over C#.