3 ms·
idk. Null in an object versus null from the database could mean something different so DBNull makes sense. Also, I don't see how you could say DBNull is respon
by 737maxtw 6y ago
idk. Null in an object versus null from the database could mean something different so DBNull makes sense.
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.
- recursive 6y ago> Null in an object versus null from the database could mean something different so DBNull makes sense. 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).
- manigandham 6y agoIf you're talking about value types then nullable value types exist, which are still structs: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...
- recursive 6y agoNone of that helps. A nullable int is `Nullable<int>` or `int?` for short. There is nothing about the language feature that allows you to represent null with a totally different type like `DBNull`, other than declaring your variable `object`. What's the return type of a method that sometimes returns `int` and sometimes `DBNull`? It can only be `object`. 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; } }
- manigandham 6y agoWhy do you need to represent `null` with another type? Is this just an issue with DB query results? You can avoid it by making sure the query never returns null or read the values yourself using a type cast test: int? someNumber = resultset.Records[0]["some_column"] as int?;
- recursive 6y agoSomehow we've gone full circle, and you're arguing my position back to me. That code sample is exactly how I'd write it. My only problem is that DBNull ever existed in the first place.
- 737maxtw 6y agoThere's a difference in certain contexts between 'null' (i.e., a reader that is not initialized) versus DBNull (the value has been set as null by the db or something else). I still think its technically correct. 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.
- recursive 6y agoFor callers that care about the distinction, there should (IMHO) have been a GetNullFlavor(). Personally, the number of times I've actually cared is indistinguishable from zero. But the number of times I've had to deal with the infectious stench of DBNull is higher than zero. Yes, I have a point of view.
- manigandham 6y agoMy point is that DBNull is only an edge case with DB queries. You can avoid ever dealing with it as I've shown or use an ORM like Dapper or Entity Framework that also handle it for you. There's really no reason why you have to touch DBNull at all.
- 6y ago