0

This question is specific for the case of get; set in properties.

Here is the code that I have:

public abstract partial class BaseViewModel : ObservableObject
{
    string? title = string.Empty;

    /// <summary>
    /// Gets or sets the title.
    /// </summary>
    /// <value>The title.</value>
    public string? Title
    {
        get => title;
        set => SetProperty(ref title, value);
    }

I understand there is a lot of information out there on the fact that a warning message is now issued for nullable reference properties and in this case the warning appears under the ? in the above code.

CS8632: The annotation for nullable reference types should only be used in code with a #nullable annotation context

However can someone tell me if this should be a concern for this particular case and if it's something I can supress for properties or should I change the way my property backing field is defined?

Richard A
  • 191
  • 7
  • Do you want to use nullable reference types or not? If you do, follow [this question](https://stackoverflow.com/questions/53633538/how-to-enable-nullable-reference-types-feature-of-c-sharp-8-0-for-the-whole-proj). Otherwise don't use `?` to mark your types. – Sweeper Mar 03 '21 at 10:02
  • The code is taken from a nuget with over 1 million downloads so I am not sure it is just me but many others using this code. I am just unclear about the consequences of saying I want or don't want to use them. – Richard A Mar 03 '21 at 10:06
  • Although it's originally set to = string.Empty; I assume it's very possible that someone would write Title = null so the answer would be yes I think it's a valid use but probably it should only be allowed in this case. Also I am unclear what would be the risks of allowing it without the warning. – Richard A Mar 03 '21 at 10:08
  • 1
    You mean this is not your code? It's not your problem then :) Report it to the authors and wait for them to fix it. In the mean time, there won't be nullable reference type checking from that NuGet package. – Sweeper Mar 03 '21 at 10:11
  • I will do that but I was trying to get some more background advice. At this time I am not even sure it's something I should be mentioning to the devs: https://www.nuget.org/packages/Refractored.MvvmHelpers/ – Richard A Mar 03 '21 at 10:19
  • @RichardA Hi, if you have solved this, remember to update your solution or ideas in answer. – Junior Jiang Mar 04 '21 at 02:13

0 Answers0