Recently I was upgrading a library to use .NET 6 and .NET 7, and there was some code that was setting the apartment state to STA, but I found out that setting the apartment state is a Windows only thing. To make the compiler happy we changed the code to this
#if NET472
_thread.SetApartmentState(ApartmentState.STA);
#else
if (OperatingSystem.IsWindows())
_thread.SetApartmentState(ApartmentState.STA);
#endif
But that got me wondering, if this code did eventually run on a Linux or MacOS platform would everything be ok? I have very little understanding of what a single threaded apartment vs multi threaded apartment does inside of Windows other than if you have a thread that should be running single threaded you should use STA. I have even less of an understanding of how these concepts translate over to Linux and MacOS and if it even matters. Do those operating systems always behave as if it's STA, or does it always behave as if it's MTA, or does it just not matter at all because it's handled so differently it doesn't even make sense to make the comparison, but it will still work.
We don't have any immediate use cases to be running this library on Linux or MacOS, but maybe one day we will, but because of that this will likely go untested for a very long time.
I'm not exactly an expert on this, but I can make some inferences based on this information in the remarks section of the ApartmentState Enum documentation:
The .NET Framework does not use apartments, and managed objects are responsible for using all shared resources in a thread-safe manner themselves.
Because COM classes use apartments, the common language runtime needs to create and initialize an apartment when calling a COM object in a COM interop situation.
This tells me the library is very likely using COM Interop for some purpose or another, and therefore answer to whether this will work on MacOS/Linux is probably, "No", since COM is a Windows thing.
But again, I'm not an expert here and maybe WINE would surprise me.