4 ms·
> I've been resorting to using bool as the generic type argument and discarding the value in these cases. After seeing other libraries do so, I've fallen into
by csharptwdec19 6y ago
> I've been resorting to using bool as the generic type argument and discarding the value in these cases.
After seeing other libraries do so, I've fallen into the habit of using a singleton 'Unit' or 'Empty' Type. (This technically also falls in line with the .NET conventions of DbNull.Value and Missing.Value)
Actually, typing that out, I wonder if we all should have been using Missing.Value all along? :)
- CodesInChaos 6y agoPity that `void` is so special in .net, instead of being a proper unit type (would have avoided the Func/Action duplication as well)
- recursive 6y agoDBNull is my least favorite part of any version of any .net thing I've ever seen. I hate it with a fiery passion. It forces otherwise sensibly typed variables to be `object`, among a host of other sins.
- 737maxtw 6y agoidk. 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.
- pjgalbraith 6y agoYeah I definitely miss having an Option<T> or Result<T> type in C# like F# has. I also don't like that DBNull and Missing are reference types, it would be better to use a value type for this to avoid having to do null checks similar to Nullable<T> but I guess in this case it doesn't matter since they are used for different purposes.