Search code examples
c#nullable-reference-types

How to all #nullable disable to all files in solution


I want to migrate my codebase to nullable references. One of migration strategies consists of adding #nullable disable prefix to all files. How I can do it automatically?


Solution

  • One way is to:

    1. Go to "Replace in Files"
    2. Check "Use regular expressions"
    3. Set the "Find" box to ^(?<![\n])
    4. Set the "Replace" box to #nullable disable\n
    5. Set the "File types" to at least contain *.cs
    6. Test carefully! I like to do a "Find All" first to make sure the matches are what I want, and use a backup/version control.

    Screenshot of the above

    ^(?<![\n]) is a regex which matches the start of a line (^) which isn't preceeded by a newline ((?<![\n])). In other words, the start of the first line of a file.