Skip to main content
Tweeted twitter.com/StackSoftEng/status/913722007119974402
added 4 characters in body
Source Link
Santhos
  • 341
  • 1
  • 3
  • 8

You are writing a boolean expression that might look like this:

team.Category == "A Team" && team?.Manager?.IsVietnamVet

public class Manager
{
    public bool IsVietnamVet { get; set; }
}

public class Team
{
    public string Category { get; set; }

    public Manager Manager { get; set; }
}

...and you get an error:

Operator '&&' cannot be applied to operands of type 'bool' and 'bool?'

What is the optimal/cleanest way to handle it?

  1. team.Category == "A Team" && (team?.Manager?.IsVietnamVet ?? false)

Is that really readable?

  1. team.Category == "A Team" && (team?.Manager?.IsVietnamVet).GetValueOrDefault()

It may not work in LINQ-to-Entities...

  1. team.Category == "A Team" && team?.Manager?.IsVietnamVet == true

Would you really write if (condition == true) without any hesitation?

Are there any other options? Is it ultimately better to write:

team.Category == "A Team" && team.Manager != null && team.Manager.IsVietnamVet

  1. team.Category == "A Team" && team.Manager != null && team.Manager.IsVietnamVet

You are writing a boolean expression that might look like this:

team.Category == "A Team" && team?.Manager?.IsVietnamVet

public class Manager
{
    public bool IsVietnamVet { get; set; }
}

public class Team
{
    public string Category { get; set; }

    public Manager Manager { get; set; }
}

...and you get an error:

Operator '&&' cannot be applied to operands of type 'bool' and 'bool?'

What is the optimal/cleanest way to handle it?

  1. team.Category == "A Team" && (team?.Manager?.IsVietnamVet ?? false)

Is that really readable?

  1. team.Category == "A Team" && (team?.Manager?.IsVietnamVet).GetValueOrDefault()

It may not work in LINQ-to-Entities...

  1. team.Category == "A Team" && team?.Manager?.IsVietnamVet == true

Would you really write if (condition == true) without any hesitation?

Are there any other options? Is it ultimately better to write:

team.Category == "A Team" && team.Manager != null && team.Manager.IsVietnamVet

You are writing a boolean expression that might look like this:

team.Category == "A Team" && team?.Manager?.IsVietnamVet

public class Manager
{
    public bool IsVietnamVet { get; set; }
}

public class Team
{
    public string Category { get; set; }

    public Manager Manager { get; set; }
}

...and you get an error:

Operator '&&' cannot be applied to operands of type 'bool' and 'bool?'

What is the optimal/cleanest way to handle it?

  1. team.Category == "A Team" && (team?.Manager?.IsVietnamVet ?? false)

Is that really readable?

  1. team.Category == "A Team" && (team?.Manager?.IsVietnamVet).GetValueOrDefault()

It may not work in LINQ-to-Entities...

  1. team.Category == "A Team" && team?.Manager?.IsVietnamVet == true

Would you really write if (condition == true) without any hesitation?

Are there any other options? Is it ultimately better to write:

  1. team.Category == "A Team" && team.Manager != null && team.Manager.IsVietnamVet
added 226 characters in body
Source Link
Santhos
  • 341
  • 1
  • 3
  • 8

You are writing a boolean expression that might look like this:

team.Category == "A Team" && team?.Manager?.IsVietnamVet

public class Manager
{
    public bool IsVietnamVet { get; set; }
}

public class Team
{
    public string Category { get; set; }

    public Manager Manager { get; set; }
}

...and you get an error:

Operator '&&' cannot be applied to operands of type 'bool' and 'bool?'

What is the optimal/cleanest way to handle it?

  1. team.Category == "A Team" && (team?.Manager?.IsVietnamVet ?? false)

Is that really readable?

  1. team.Category == "A Team" && (team?.Manager?.IsVietnamVet).GetValueOrDefault()

It may not work in LINQ-to-Entities...

  1. team.Category == "A Team" && team?.Manager?.IsVietnamVet == true

Would you really write if (condition == true) without any hesitation?

Are there any other options? Is it ultimately better to write:

team.Category == "A Team" && team.Manager != null && team.Manager.IsVietnamVet

You are writing a boolean expression that might look like this:

team.Category == "A Team" && team?.Manager?.IsVietnamVet

...and you get an error:

Operator '&&' cannot be applied to operands of type 'bool' and 'bool?'

What is the optimal/cleanest way to handle it?

  1. team.Category == "A Team" && (team?.Manager?.IsVietnamVet ?? false)

Is that really readable?

  1. team.Category == "A Team" && (team?.Manager?.IsVietnamVet).GetValueOrDefault()

It may not work in LINQ-to-Entities...

  1. team.Category == "A Team" && team?.Manager?.IsVietnamVet == true

Would you really write if (condition == true) without any hesitation?

Are there any other options? Is it ultimately better to write:

team.Category == "A Team" && team.Manager != null && team.Manager.IsVietnamVet

You are writing a boolean expression that might look like this:

team.Category == "A Team" && team?.Manager?.IsVietnamVet

public class Manager
{
    public bool IsVietnamVet { get; set; }
}

public class Team
{
    public string Category { get; set; }

    public Manager Manager { get; set; }
}

...and you get an error:

Operator '&&' cannot be applied to operands of type 'bool' and 'bool?'

What is the optimal/cleanest way to handle it?

  1. team.Category == "A Team" && (team?.Manager?.IsVietnamVet ?? false)

Is that really readable?

  1. team.Category == "A Team" && (team?.Manager?.IsVietnamVet).GetValueOrDefault()

It may not work in LINQ-to-Entities...

  1. team.Category == "A Team" && team?.Manager?.IsVietnamVet == true

Would you really write if (condition == true) without any hesitation?

Are there any other options? Is it ultimately better to write:

team.Category == "A Team" && team.Manager != null && team.Manager.IsVietnamVet

Source Link
Santhos
  • 341
  • 1
  • 3
  • 8

Optimal way to use null conditional operators in boolean expressions

You are writing a boolean expression that might look like this:

team.Category == "A Team" && team?.Manager?.IsVietnamVet

...and you get an error:

Operator '&&' cannot be applied to operands of type 'bool' and 'bool?'

What is the optimal/cleanest way to handle it?

  1. team.Category == "A Team" && (team?.Manager?.IsVietnamVet ?? false)

Is that really readable?

  1. team.Category == "A Team" && (team?.Manager?.IsVietnamVet).GetValueOrDefault()

It may not work in LINQ-to-Entities...

  1. team.Category == "A Team" && team?.Manager?.IsVietnamVet == true

Would you really write if (condition == true) without any hesitation?

Are there any other options? Is it ultimately better to write:

team.Category == "A Team" && team.Manager != null && team.Manager.IsVietnamVet