A questionable C# bug – Error CS0173

I’m working on a game and server for a game called “Word Chain” which is based on Chain Reaction. While scouring my code JetBrains ReSharper told me that a section of my code was redundant. ReSharper was sort-of right, but was actually quite wrong according to Roslyn. In fact that only reason the code looked the way it did was because of a bizarre “bug” in my code that was anything but that.

Here’s what was written down (UDPGameServer and TCPGameServer stem from abstract class GameServer)

gameServer = Program.SpArguments.ContainsKey("udp") ? (GameServer)new UDPGameServer() : (GameServer)new TCPGameServer();

This is what ReSharper recommended, but this errors out with the error message show below.

gameServer = Program.SpArguments.ContainsKey("udp") ? new UDPGameServer() : new TCPGameServer();

This is one way to cast that I tried, but failed with the error below.

(GameServer)(Program.SpArguments.ContainsKey("udp") ? new UDPGameServer() : new TCPGameServer());

This is another way to cast that I tried, but failed with the error below.

gameServer = (Program.SpArguments.ContainsKey("udp") ? new UDPGameServer() : new TCPGameServer()) as GameServer;

This is the error shown from the three changes I attempted at the code. As you might clearly see, this really shouldn’t error out and should be legal code by C# standards.

Error CS0173 Type of conditional expression cannot be determined because there is no implicit conversion between ‘ChainReactGame.UDPGameServer’ and ‘ChainReactGame.TCPGameServer’ ChainReactMPServerr

In closing I don’t have much to say other than I’d like to see a Microsoft dev tell me why they think this error is appropriate and where in the C# standards can I find a specification that says this error should be thrown by the roslyn compiler. I personally think this is a roslyn bug, but I don’t know. I’d like to hear your thoughts on it if you’ve been thrown this doozy.


Published by Goodlookinguy

Owner of NRGsoft, programmer, 日本語を話す人間, and UI aesthetics perfectionist.