简体   繁体   中英

Null Coalessing not returning correct value

It is my understanding that the below line should return an empty string "" in the case the Datagrid cell was empty.

string proxyuser = this.dataGridView2.SelectedRows[0].Cells[3].Value.ToString() ?? "";

For some reason, I"m still receiving a null reference error on the init line.

There's likely a chance one of the other parts are null, I recommend trying out null propagation. (where a ?. will check if the current left part is null first before continueing past the dot, otherwise, it'll return a null, and then giving back the empty string)

For example:

string proxyuser = this.dataGridView2.SelectedRows[0]?.Cells[3]?.Value?.ToString() ?? "";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM