Also, I don't see how you could say DBNull is responsible for forcing object on us. They could have just as easily made it a struct and i dont see what it would solve.
Also, I don't see how you could say DBNull is responsible for forcing object on us. They could have just as easily made it a struct and i dont see what it would solve.
Fortunately, it's super easy to tell the difference. `DatabaseQuery()` vs `SomeOtherFunction()`. See?
> Also, I don't see how you could say DBNull is responsible for forcing object on us. They could have just as easily made it a struct and i dont see what it would solve.
Doesn't matter. Even if it was a struct, the only way to store both kinds of value in one variable is to declare it an `object`, even if it was a struct. `null` is a perfectly legal value for a `string` variable, but as soon as the possibility for some other null object (or struct) is raised, the target variable must be `object`. C# doesn't do unions (as a first class language feature anyway).
https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...
I seem to be having some trouble explaining my point. Maybe this will illustrate better. What should be the declared return type of this method?
??? GetValue() {
if (condition) {
return 7;
}
else {
return DBNull.Value;
}
}int? someNumber = resultset.Records[0]["some_column"] as int?;
That code sample is exactly how I'd write it. My only problem is that DBNull ever existed in the first place.
I get that it was annoying, but most of .NET before 3.5 was anyway. It's just nobody bothered to make the right syntactic sugar.
Yes, I have a point of view.
There's really no reason why you have to touch DBNull at all.
var dt = new DataTable();
dt.Columns.Add("name", typeof(string));
//dt.Columns.Add("count", typeof(int?)); // NotSupportedException: DataSet does not support System.Nullable<>.
dt.Columns.Add("count", typeof(int));
var row=dt.NewRow();
row["name"] = null;
//row["count"] = null; // ArgumentException: Cannot set Column 'count' to be null. Please use DBNull instead.
row["count"] = DBNull.Value;
dt.Rows.Add(row);