I’m trying to use the null-coalescing operator on an int. It works when I use it on strings
UserProfile.Name = dr["Name"].ToString()??"";
When I try to use it on an int like this
UserProfile.BoardID = Convert.ToInt32(dr["BoardID"])??default(int);
I get this error message
Operator ‘??’ cannot be applied to
operands of type ‘int’ and ‘int’
I found this blog post where it is used http://davidhayden.com/blog/dave/archive/2006/07/05/NullCoalescingOperator.aspx with the int data type. Can anyone tell what I’m doing wrong?
I suspect what you’re really trying to do is set BoardID to 0 if dr[“BoardID”] is NULL from the database. Because if dr[“BoardID”] IS null, Convert.ToInt32 will fail. Try this: