1

I have read this stackoverflow topic:

Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and <null>

I understand there is a risk of the compiler not understanding one of the types in ternary operator. However, my both classes inherit the same base class.

This is what I currently have:

public async Task Handle(IClientPendingIntegrationEvent message) {
    Command<Unit> command = _requestContext.SourceType == RequestSourceType.Backoffice
            ? new CreateManualUserEventCommand(message.UserId, eventType, UserEventDomain.Registration, message.Reason)
            : new CreateUserEventCommand(message.UserId, eventType, UserEventOriginType.System, UserEventDomain.Registration, message.Reason);

    await _mediator.Send(command);
}


public class CreateManualUserEventCommand : Command<Unit>
{
    public CreateManualUserEventCommand(
        string userId,
        UserEventType userEventType,
        UserEventDomain domain,
        string comment = null)
    {
        UserId = userId;
        Type = userEventType;
        Domain = domain;
        Comment = comment;
    }

    public string UserId { get; private set; }

    public UserEventType Type { get; private set; }

    public UserEventDomain Domain { get; private set; }

    public string Comment { get; private set; }
}


public class CreateUserEventCommand : Command<Unit>
    {
        public CreateUserEventCommand(
            string userId,
            UserEventType type,
            UserEventOriginType createdByOrigin,
            UserEventDomain domain,
            string comment = null)
        {
            UserId = userId;
            Type = type;
            CreatedByOrigin = createdByOrigin;
            Domain = domain;
            Comment = comment;
        }

        public string UserId { get; }

        public UserEventType Type { get; }

        public UserEventOriginType CreatedByOrigin { get; }

        public UserEventDomain Domain { get; }

        public string Comment { get; }
    }

This is the exact error the compiler throws.

Error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between

'MFG.Auditing.Application.Features.Backoffice.UserEvents.Commands.CreateManualUserEvent.CreateManualUserEventCommand' and 'MFG.Auditing.Application.Features.System.UserEvents.Commands.CreateUserEvent.CreateUserEventCommand'

I understand that I can easily rework this with if and else but I need to use ternary operator here.

grozdeto
  • 1,201
  • 1
  • 13
  • 34
  • 1
    "However, my both classes inherit the same base class." But neither expression *is* that base class type. The compiler doesn't try to find some best common base type. Please read the accepted answer on the other question carefully - it does go through the spec. – Jon Skeet Dec 16 '21 at 08:48
  • Basically if you case *either* of the operands to the base class type, it'll be fine. – Jon Skeet Dec 16 '21 at 08:48
  • You can fix it by casting - put `(Command)` in front of either of the `new`s in the ternary statement. – Matthew Watson Dec 16 '21 at 08:49
  • 2
    Note that this was was fixed in C# 9 with target-typed conditionals: [SharpLab](https://sharplab.io/#v2:EYLgtghglgdgNAFxANwKYCcoDMCeAfAAQCYBGAWAChgB7agGwAJgGBeBhdAV1QG5KAhCAGdUDdKiGc6CVkwYB+BjFQB3BgEEAFAEoGIJaob8dfCpQIBmBsSPDRAbwYBfc1Zvq9tkQ0cuKl6yIjT0FvXyA===) – canton7 Dec 16 '21 at 08:58
  • Thank you all. Simply casting either to (Command) works. As Jon Skeet mentioned the compiler doesn't try to find some best common base type. – grozdeto Dec 16 '21 at 09:06

0 Answers0