代码之家  ›  专栏  ›  技术社区  ›  t3chb0t

指示ReSharper在其他属性为“true”时字段不为null

  •  0
  • t3chb0t  · 技术社区  · 7 年前

    Exists 返回 true 当球场 _value null

    StreamReader ReSharper 是在警告我这可能是 无效的 if(Exists) 早些时候不会。

    我用注释禁用了警告,但我想知道是否有更干净的方法或一些注释或其他技巧 再竖琴 了解使用它是安全的 什么时候 Exists == true ?

    internal class User
    {
        private readonly string _value;
    
        internal User([NotNull] string name, [CanBeNull] string value)
        {
            _value = value;
        }
    
        public bool Exists => !(_value is null);
    
        public async Task CopyToAsync(Stream stream)
        {
            if (Exists)
            {
                // ReSharper disable once AssignNullToNotNullAttribute - '_value' won't be null here
                using (var valueStream = _value.ToStreamReader()) 
                {
                    await valueStream.BaseStream.CopyToAsync(stream);
                }
            }
        }   
    }
    
    0 回复  |  直到 7 年前
    推荐文章