0

I am currently using Visual studio 2022, below is my code.

for (int i = 0; i < list.Count; i++)
        {
            if (list[i].TenantDetails != null)
                dtEntity = DataTableManager.ConvertData(list[i].TenantDetails);
        }

public static DataTable ConvertData(byte[] data) { ... }

Getting the below warning. Warning CS8604 Possible null reference argument for parameter 'data' in 'DataTable DataTableManager.ConvertData(byte[] data)'

How to avoid this warning? After converting my old project from VS 2019 to 2022 I am getting lot of code warnings.

5
  • You probably have nullable references enabled, which means you have to take extra care. What exactly and precisely is the type of the .TenantDetails member? Commented Dec 17, 2021 at 18:09
  • public partial class Tenant { public byte[]? TenantDetails { get; set; } } If I remove the "?" in byte[] it gives another warning - Warning CS8618 Non-nullable property 'TenantDetails' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. Commented Dec 17, 2021 at 18:14
  • 1
    So you've said that TenantDetails is byte[]?, the question mark at the end signals that the property may be null. Yet, ConvertData takes byte[], without the question mark. I know you have the if-statement there but the compiler is not smart enough always, so you might have to use ! after the parameter, or you can do var details = list[i].TenantDetails;, and then use that in both if-statement and parameter, then the compiler sees that it's guaranteed to be the same value. Commented Dec 17, 2021 at 18:22
  • I guess the fact that it doesn't know what reading list[i] does trips it up. Commented Dec 17, 2021 at 18:24
  • I am sure I am going to assign value to below property while intializing the object. public byte[]? TenantDetails { get; set; } But Is there a way to remove "?" and get rid of Warning CS8618 Non-nullable property 'TenantDetails' must contain a non-null value when exiting constructor Commented Dec 17, 2021 at 18:35

2 Answers 2

1

Is there a way to remove "?" and get rid of Warning CS8618 Non-nullable property 'TenantDetails' must contain a non-null value when exiting constructor

You can try to set default value for TenantDetails.

public byte[] TenantDetails { get; set; } =new byte[0];

And you can try to check TenantDetails.Length:

for (int i = 0; i < list.Count; i++)
        {
            if (list[i].TenantDetails.Length>0)
                dtEntity = DataTableManager.ConvertData(list[i].TenantDetails);
        }
Sign up to request clarification or add additional context in comments.

Comments

0

I found a way to get rid of these warnings using a .editorconfig file.

Go to a warning a position the cursor to see the possible solutions, there are two options: "suppress on source" or the below one which is like suppress in the solution. Click on that one and an .editor config file will be created for you and the warning will just gone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.