Post

How to suppress piece of code?

  • For class and members
1
 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
  • inside a method
1
2
3
 #pragma warning disable CS0618 // Type or member is obsolete
 //code
 #pragma warning restore CS0618 // Type or member is obsolete
  • Using props and parameter

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/nullable-analysis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Test
{
    [Fact]
    private void Run()
    {
        var s = new StaticAnalisisAttr();
        s.ScreenName =
            null; // without [AllowNull] over `ScreenName` get `Warning CS8625 : Cannot convert null literal to non-nullable reference type.`
    }
}

internal class StaticAnalisisAttr
{
    [AllowNull]
    public string ScreenName
    {
        get => _screenName;
        set => _screenName = value ?? "temp";
    }

    private string _screenName = "temp";
}
This post is licensed under CC BY 4.0 by the author.