nullable

C# DBNull and nullable Types - cleanest form of conversion

こ雲淡風輕ζ 提交于 2019-12-18 10:39:31
问题 I have a DataTable, which has a number of columns. Some of those columns are nullable. DataTable dt; // Value set. DataRow dr; // Value set. // dr["A"] is populated from T-SQL column defined as: int NULL What, then, is the cleanest form of converting from a value in a DataRow, to a nullable variable. Ideally, I would be able to do something like: int? a = dr["A"] as int?; Edit : Turns out you CAN do this, the side effect being that if your Schema types arn't ints, then this is ALWAYS going to

Is there any difference between type? and Nullable<type>?

帅比萌擦擦* 提交于 2019-12-18 03:01:29
问题 In C# are the nullable primitive types (i.e. bool? ) just aliases for their corresponding Nullable<T> type or is there a difference between the two? 回答1: If you look at the IL using Ildasm, you'll find that they both compile down to Nullable<bool> . 回答2: There is no difference between bool? b = null and Nullable<bool> b = null . The ? is just C# compiler syntax sugar. 回答3: To access the value of the bool? you need to do the following: bool? myValue = true; bool hasValue = false; if (myValue

Is there any difference between type? and Nullable<type>?

馋奶兔 提交于 2019-12-18 03:01:15
问题 In C# are the nullable primitive types (i.e. bool? ) just aliases for their corresponding Nullable<T> type or is there a difference between the two? 回答1: If you look at the IL using Ildasm, you'll find that they both compile down to Nullable<bool> . 回答2: There is no difference between bool? b = null and Nullable<bool> b = null . The ? is just C# compiler syntax sugar. 回答3: To access the value of the bool? you need to do the following: bool? myValue = true; bool hasValue = false; if (myValue

Is using Optional.ofNullable as a replacement for the ternary operator a good practice?

自古美人都是妖i 提交于 2019-12-18 02:47:10
问题 Consider the usage of this expression: String hi = Optional.ofNullable(sayHi()).orElse("-"); which effectively corresponds to this ternary expression: String hi = sayHi() != null ? sayHi() : "-"; Is this usage of Optional.ofNullable with a method call a good practice? Or just extra verbose coding? I recognise that Optional.ofNullable actually creates a variable and avoids calling the sayHi() method twice. To avoid this problem you actually could create an extra variable but this adds to the

Is using Optional.ofNullable as a replacement for the ternary operator a good practice?

这一生的挚爱 提交于 2019-12-18 02:47:08
问题 Consider the usage of this expression: String hi = Optional.ofNullable(sayHi()).orElse("-"); which effectively corresponds to this ternary expression: String hi = sayHi() != null ? sayHi() : "-"; Is this usage of Optional.ofNullable with a method call a good practice? Or just extra verbose coding? I recognise that Optional.ofNullable actually creates a variable and avoids calling the sayHi() method twice. To avoid this problem you actually could create an extra variable but this adds to the

Nullable GUID

≡放荡痞女 提交于 2019-12-18 02:02:14
问题 In my database, in one of the table I have a GUID column with allow nulls. I have a method with a Guid? parameter that inserts a new data row in the table. However when I say myNewRow.myGuidColumn = myGuid I get the following error: "Cannot implicitly convert type 'System.Guid?' to 'System.Guid'." 回答1: The ADO.NET API has some problems when it comes to handling nullable value types (i.e. it simply doesn't work correctly). We've had no end of issues with it, and so have arrived at the

Find type of nullable properties via reflection

只愿长相守 提交于 2019-12-17 22:32:05
问题 I examine the properties of an object via reflection and continue processing the data type of each property. Here is my (reduced) source: private void ExamineObject(object o) { Type type = default(Type); Type propertyType = default(Type); PropertyInfo[] propertyInfo = null; type = o.GetType(); propertyInfo = type.GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); // Loop over all properties for (int propertyInfoIndex = 0;

What does the ? mean after a type? [duplicate]

谁说胖子不能爱 提交于 2019-12-17 20:45:23
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: a curious c# syntax So I've seen some code around and a few of them use a ? after the type, like this: private Point? loc = null; So I'm wondering if Point? is different than Point (can't put a question mark at the end of my sentence or I'll confuse you guys ... :] ). The language I'm using is C# by the way. 回答1: T? is a shorthand (in C#) for Nullable<T> - so Point? is another way of writing Nullable<Point> or

Nullable<T>: and overloaded operators, bool? & bool

蓝咒 提交于 2019-12-17 19:44:33
问题 Why is operator '&' defined for bool?, and operator '&&' is not? How exactly does this 1) bool? & bool? and 2) bool? and bool work? Any other "interesting" operator semantics on Nullable? Any overloaded operators for generic T? 回答1: Operators on Nullable<T> are "lifted" operators. What this means is: if T has the operator, T? will have the "lifted" counterpart. && and || aren't really operators in the same sense as & and | - for example, they can't be overloaded - from the ECMA spec 14.2.2

Why does the == operator work for Nullable<T> when == is not defined?

戏子无情 提交于 2019-12-17 19:38:22
问题 I was just looking at this answer, which contains the code for Nullable<T> from .NET Reflector, and I noticed two things: An explicit conversion is required when going from Nullable<T> to T . The == operator is not defined. Given these two facts, it surprises me that this compiles: int? value = 10; Assert.IsTrue(value == 10); With the code value == 10 , either value is being magically converted to an int (hence allowing int 's == operator to be used, or the == operator is being magically