C# 12's Secret Feature | .NET Tips 8

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

I found this feature by accident. I had to write some empty classes and structs to use with code generators, and I wrote them as I would in Rust, with just the semicolon after the name. A few minutes later when it successfully compiled I thought to myself "Wait, that shouldn't have worked"

petrusion
Автор

Combined with primary constructors and how records have... always worked - this is a change that actually makes a lot of sense.

an_wobbly
Автор

Most people will find them useless, but in fact empty classes do sometimes come in handy. For one, they are often used as 'tags', e.g. in Unity's DOTS or in DI, for two, they work really well with source generation, where the only thing you do is mark it with 'partial' and an attribute and then the magic stuff just happens.

promant
Автор

Been doing this and it really helps keep the code base clean sometimes. Good feature, should have been around since C# 1.

ethanrushbrook
Автор

One legitimate use I had for an empty class was when I was implementing an API for communicating with a proprietary Bluetooth peripheral. Our library defined “commands”, which were requests the peripheral could fulfill. Each included logic for decoding its response. All commands implemented a common interface with a strongly typed response. But some commands did not return a response. So we made those return a dummy type (i.e. an empty class called NullResponse).

We could have made commands that return no response implement some different interface, but we would have lost the ability to provide APIs that chain commands that return responses intermixed with commands that don’t.

jondoty
Автор

Empty types are useful to overload methods. However, named parameters and default values are usually better. Empty interfaces are used to "tag" classes, which is useful when you work with additional preprocessors and auxillary compilers.

pi_xi
Автор

Didn’t know {} was previously mandatory.
Used ; recently to create a marker interface. It felt just natural.

ryan-heath
Автор

Use this all the time for marker interfaces and records (esp. commands/events). So much tidier!

cward
Автор

Really useful when you interop with unmanaged code and you need strongly typed opaque pointers.

nitrous
Автор

it's been introduced for records but very useful for other types as well

Kitulous
Автор

I use empty types all the time, it's for event driven system - sometimes the event doe not have any additional data with it.

markovcd
Автор

This seems good, I use empty structs a lot for typing pointers.

funkydiddykong
Автор

sounds like a bug that slipped by and turned out to be a feature 😂😂

hadahda
Автор

yeah i know that function. I use both the brackets and the semicolon.

Shocked_Creative
Автор

Well the no body part is new to me. But isn't empty class always been there? I have been using it for quite a while.

asagiai
Автор

i have so many empty sub classes in my code waiting for such a feature.

vothaison
Автор

Since we have file scoped name spaces, can we have file scoped classes already...?

ronsijm
Автор

I just used this last week, didn't even realize it did not exist in C#11.

MahbuburRahman-ucnp
Автор

I figured it out on my own. I was curious what would happen if I swapped out those curly brackets for a semicolon.

ed-jamal
Автор

I don't ever remember see a case where an empty class / struct / interface would be useful. Perhaps code generators?

Sergio_Loureiro