nullable

How do I use DateTime.TryParse with a Nullable<DateTime>?

不羁的心 提交于 2019-12-17 10:30:44
问题 I want to use the DateTime.TryParse method to get the datetime value of a string into a Nullable. But when I try this: DateTime? d; bool success = DateTime.TryParse("some date text", out (DateTime)d); the compiler tells me 'out' argument is not classified as a variable Not sure what I need to do here. I've also tried: out (DateTime)d.Value and that doesn't work either. Any ideas? 回答1: DateTime? d=null; DateTime d2; bool success = DateTime.TryParse("some date text", out d2); if (success) d=d2;

Nullable type with inline if cannot work together?

好久不见. 提交于 2019-12-17 07:49:26
问题 Given the following code: Dim widthStr As String = Nothing This works - width is assigned Nothing : Dim width As Nullable(Of Double) If widthStr Is Nothing Then width = Nothing Else width = CDbl(widthStr) End If But this does not - width becomes 0.0 (although it seems to be logically identical code): Dim width As Nullable(Of Double) = If(widthStr Is Nothing, Nothing, CDbl(widthStr)) Why? Is there anything I can do to make it work? 回答1: This all comes down to type analysis of expressions.

Nullable type with inline if cannot work together?

白昼怎懂夜的黑 提交于 2019-12-17 07:49:15
问题 Given the following code: Dim widthStr As String = Nothing This works - width is assigned Nothing : Dim width As Nullable(Of Double) If widthStr Is Nothing Then width = Nothing Else width = CDbl(widthStr) End If But this does not - width becomes 0.0 (although it seems to be logically identical code): Dim width As Nullable(Of Double) = If(widthStr Is Nothing, Nothing, CDbl(widthStr)) Why? Is there anything I can do to make it work? 回答1: This all comes down to type analysis of expressions.

In C# why can't a conditional operator implicitly cast to a nullable type

前提是你 提交于 2019-12-17 07:27:46
问题 I am curious as to why an implicit cast fails in... int? someValue = SomeCondition ? ResultOfSomeCalc() : null; and why I have to perform an explicit cast instead int? someValue = SomeCondition ? ResultofSomeCalc() : (int?)null; It seems to me that the compiler has all the information it need to make an implicit casting decision, no? 回答1: The relevant section of the C# 3.0 spec is 7.13, the conditional operator: The second and third operands of the ?: operator control the type of the

How much disk-space is needed to store a NULL value using postgresql DB?

无人久伴 提交于 2019-12-17 07:25:44
问题 let's say I have a column on my table defined the following: "MyColumn" smallint NULL Storing a value like 0, 1 or something else should need 2 bytes (1). But how much space is needed if I set "MyColumn" to NULL? Will it need 0 bytes? Are there some additional needed bytes for administration purpose or such things for every column/row? (1) http://www.postgresql.org/docs/9.0/interactive/datatype-numeric.html 回答1: Null columns are not stored. The row has a bitmap at the start and one bit per

How is the boxing/unboxing behavior of Nullable<T> possible?

前提是你 提交于 2019-12-17 06:14:14
问题 Something just occurred to me earlier today that has got me scratching my head. Any variable of type Nullable<T> can be assigned to null . For instance: int? i = null; At first I couldn't see how this would be possible without somehow defining an implicit conversion from object to Nullable<T> : public static implicit operator Nullable<T>(object box); But the above operator clearly does not exist, as if it did then the following would also have to be legal, at least at compile-time (which it

How is the boxing/unboxing behavior of Nullable<T> possible?

浪子不回头ぞ 提交于 2019-12-17 06:14:13
问题 Something just occurred to me earlier today that has got me scratching my head. Any variable of type Nullable<T> can be assigned to null . For instance: int? i = null; At first I couldn't see how this would be possible without somehow defining an implicit conversion from object to Nullable<T> : public static implicit operator Nullable<T>(object box); But the above operator clearly does not exist, as if it did then the following would also have to be legal, at least at compile-time (which it

Why is this code invalid in C#?

别来无恙 提交于 2019-12-17 06:06:35
问题 The following code will not compile: string foo = "bar"; Object o = foo == null ? DBNull.Value : foo; I get: Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string' To fix this, I must do something like this: string foo = "bar"; Object o = foo == null ? DBNull.Value : (Object)foo; This cast seems pointless as this is certainly legal: string foo = "bar"; Object o = foo == null ? "gork" : foo; It seems to me that

Deserializing empty xml attribute value into nullable int property using XmlSerializer

流过昼夜 提交于 2019-12-17 04:27:41
问题 I get an xml from the 3rd party and I need to deserialize it into C# object. This xml may contain attributes with value of integer type or empty value: attr=”11” or attr=””. I want to deserialize this attribute value into the property with type of nullable integer. But XmlSerializer does not support deserialization into nullable types. The following test code fails during creation of XmlSerializer with InvalidOperationException {"There was an error reflecting type 'TestConsoleApplication

What does “DateTime?” mean in C#?

為{幸葍}努か 提交于 2019-12-17 02:29:40
问题 I am reading a .NET book, and in one of the code examples there is a class definition with this field: private DateTime? startdate What does DateTime? mean? 回答1: Since DateTime is a struct , not a class , you get a DateTime object , not a reference , when you declare a field or variable of that type. And, in the same way as an int cannot be null , so this DateTime object can never be null , because it's not a reference. Adding the question mark turns it into a nullable type, which means that