I have the following piece of code with warning Expression is always true
:
public class MyClass
{
private readonly IHubContext<NotificationHub> _hubContext;
public MyClass(
IHubContext<NotificationHub> hubContext)
{
_logHandler = logHandler;
_hubContext = hubContext;
}
foreach (string group in groups)
{
IClientProxy connection = _hubContext.Clients.User(group);
if (connection != null) // Expression is always true
{
}
}
}
I know that Resharper is very smart, however I cannot understand why it says Expression is always true
as IClientProxy
is reference type.
IClientProxy
resides in Microsoft.AspNetCore.SignalR
namespace.
Do you know why Resharper says Expression is always true
?
This is how User
is declared in public interface IHubClients<T>
:
public interface IHubClients<T>
{
/// <summary>
/// Gets a <typeparamref name="T" /> that can be used to invoke
/// methods on all connections associated with the specified user.
/// </summary>
/// <param name="userId">The user ID.</param>
/// <returns>A client caller.</returns>
T User(string userId);
}
UPDATE:
If I add ?
sign:
IClientProxy? connection = _hubContext.Clients.User(group);
Then I will get the following warning:
The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
UPDATE 1
Even if I use var
:
var IClientProxy? connection = _hubContext.Clients.User(group);
if (connection != null)
then warning is arised Expression is always true
: