nullable

Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type

二次信任 提交于 2020-02-01 04:40:06
问题 Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type. And similarly, Boxed nullable enum can be cast to underlying type but boxed underlying type can't be cast to nullable enum. Ok, I know "boxed nullable type" is not the best way to describe it, but it's for the sake of the question. I'm aware it's the underlying value type that's getting boxed. I will show it with examples. Assume I have an enum with int as the underlying type. enum Sex {

Why does TargetNullValue update nullable Source

二次信任 提交于 2020-01-29 06:59:25
问题 TargetNullValue is supposed to update the binding Target when the binding Source evaluates to null : Gets or sets the value that is used in the target when the value of the source is null. In addition to that it also appears to set the Source to null (if possible), when the value of Target is equals to given TargetNullValue . In other words, it effectively sets up an equivalency between null and the TargetNullValue property's value. However this is not mentioned in the documentation at all.

Receiving error about nullable type parameter even when parameter has notnull constraint

安稳与你 提交于 2020-01-24 16:38:48
问题 I have a generic interface IDataAdapter<T> ; implementors of the interface should be able to read POCOs with Guid IDs from a data source. IDataAdapter<T> has a method Read(Guid id) which I want to return a T? , where null indicates that no matches were found in the data source. However, even with a constraint T : notnull on IDataAdapter<T> , attempting to define this method gives the error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type.

Receiving error about nullable type parameter even when parameter has notnull constraint

泄露秘密 提交于 2020-01-24 16:38:07
问题 I have a generic interface IDataAdapter<T> ; implementors of the interface should be able to read POCOs with Guid IDs from a data source. IDataAdapter<T> has a method Read(Guid id) which I want to return a T? , where null indicates that no matches were found in the data source. However, even with a constraint T : notnull on IDataAdapter<T> , attempting to define this method gives the error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type.

Receiving error about nullable type parameter even when parameter has notnull constraint

送分小仙女□ 提交于 2020-01-24 16:38:06
问题 I have a generic interface IDataAdapter<T> ; implementors of the interface should be able to read POCOs with Guid IDs from a data source. IDataAdapter<T> has a method Read(Guid id) which I want to return a T? , where null indicates that no matches were found in the data source. However, even with a constraint T : notnull on IDataAdapter<T> , attempting to define this method gives the error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type.

Null-Coallescing Operator - Why Casting?

牧云@^-^@ 提交于 2020-01-24 05:38:05
问题 Can anyone please tell me why does the first of the following statements throws a compilation error and the second one does not? NewDatabase.AddInParameter(NewCommand, "@SomeString", DbType.String, SomeString ?? DBNull.Value); // <-- Throws compilation error! NewDatabase.AddInParameter(NewCommand, "@SomeString", DbType.String, (object)(SomeString) ?? DBNull.Value); // <-- Compiles! I tried other nullable types such as byte? and got the same result. Can anyone please tell me why do I need to

REST with nullable types?

狂风中的少年 提交于 2020-01-23 05:22:16
问题 I've hit a brickwall. My REST implementation won't accept Nullable values. [OperationContract] [WebInvoke(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Transactions?AccNo={AccNo}&CostCentreNo={CostCentreNo}&TransactionType={TransactionType}&Outstanding={Outstanding}&CheckStartDate={CheckStartDate}&CheckEndDate={CheckEndDate}")] List<Transactions> GetTransactions(Int32 AccNo, Int32 CostCentreNo, Int32 TransactionType, Boolean Outstanding, DateTime? CheckStartDate, DateTime?

How to create structure with null value support?

做~自己de王妃 提交于 2020-01-23 04:45:18
问题 I'm new in C#. In c# I can't set value of a structure to null how can I create a structure with null value support? 回答1: Structs and value types can be made nullable by using the Generic Nullable<> class to wrap it. For instance: Nullable<int> num1 = null; C# provides a language feature for this by adding a question mark after the type: int? num1 = null; Same should work for any value type including structs. MSDN Explanation: Nullable Types (c#) 回答2: You can use Nullable<T> which has an alias

Can I cast from DBNull to a Nullable Bool in one line?

混江龙づ霸主 提交于 2020-01-22 17:20:12
问题 I have a database query which will either return NULL or a boolean (bit) value. I wish to store this value in a variable of type Nullable<bool> in C#. I can't seem to find an acceptable mix of explict casts and conversions that do this in a simple way without Exceptions being thrown. Can it be done in one readable line? EDIT: Code as requested private Nullable<bool> IsRestricted; ...//data access IsRestricted = (bool?)DataBinder.GetPropertyValue(dataObj, "IsRestricted"); or perhaps

Alternatives to nullable types in C#

坚强是说给别人听的谎言 提交于 2020-01-22 10:56:05
问题 I am writing algorithms that work on series of numeric data, where sometimes, a value in the series needs to be null. However, because this application is performance critical, I have avoided the use of nullable types. I have perf tested the algorithms to specifically compare the performance of using nullable types vs non-nullable types, and in the best case scenario nullable types are 2x slower, but often far worse. The data type most often used is double, and currently the chosen