nullable

Is it possible to use operator ?? and throw new Exception()?

别说谁变了你拦得住时间么 提交于 2019-11-30 21:31:20
问题 I have a number of methods doing next: var result = command.ExecuteScalar() as Int32?; if(result.HasValue) { return result.Value; } else { throw new Exception(); // just an example, in my code I throw my own exception } I wish I could use operator ?? like this: return command.ExecuteScalar() as Int32? ?? throw new Exception(); but it generates a compilation error. Is it possible to rewrite my code or there is only one way to do that? 回答1: For C# 7 In C# 7, throw becomes an expression, so it's

C# Performance gain returning a Nullable Type from a SqlDataReader

ぐ巨炮叔叔 提交于 2019-11-30 20:10:01
I have a simple method that returns a Nullable Int32 from a DataReader rather than the built in GetInt32. I am calling this method many many times and have one situation where any time I can shave off of it would be beneficial. Can anyone suggest any alternative and faster ways of getting a nullable Int32 out of the DataReader? private Int32? GetNullableInt32(SqlDataReader dataReader, int fieldIndex) { return !dataReader.IsDBNull(fieldIndex) ? dataReader.GetInt32(fieldIndex) : (Int32?)null; } I seem to recall that it can sometimes by faster to get the value as an object and then check if that

Bug?? If you assign a value to a nullable integer via a ternary operator, it can't become null

北城余情 提交于 2019-11-30 19:55:44
问题 dim val1 As Integer? = If(5 > 2, Nothing, 43) ' val1 = 0 dim val1 As Integer? = If(5 > 2, Nothing, Nothing) ' val1 = Nothing What gives? Is this a bug, or am I overlooking something? 回答1: The problem is that Nothing in VB.NET works differently than, for example, null in C#. When Nothing is used in the context of a value type (such as Integer ) it represents the default value of that type. In this case, that's 0. In your first example, both branches of the ternary operator are valid Integer

Variable type ending with ?

限于喜欢 提交于 2019-11-30 18:53:43
What does ? mean: public bool? Verbose { get; set; } When applied to string? , there is an error: The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable' ? makes your non-nullable (value) types nullable. It doesn't work for string , as it is reference type and therefore nullable by default. From MSDN , about value types: Unlike reference types, a value type cannot contain the null value. However, the nullable types feature does allow for values types to be assigned to null. ? is basically a shorthand for Nullable<T

Wrong compiler warning when comparing struct to null

谁说胖子不能爱 提交于 2019-11-30 18:12:24
Consider the following code: DateTime t = DateTime.Today; bool isGreater = t > null; With Visual Studio 2010 (C# 4, .NET 4.0), I get the following warning: warning CS0458: The result of the expression is always 'null' of type 'bool?' This is incorrect; the result is always false (of type bool ): Now, the struct DateTime overloads the > (greater than) operator. Any non-nullable struct (like DateTime) is implicitly convertible to the corresponding Nullable<> type. The above expression is exactly equivalent to bool isGreater = (DateTime?)t > (DateTime?)null; which also generates the same wrong

How Does .Net Allow Nullables To Be Set To Null

白昼怎懂夜的黑 提交于 2019-11-30 17:31:21
问题 The Nullable<T> type is defined as a struct . In .Net, you can't assign null to a struct because structs are value types that cannot be represented with null (with the exception of Nullable<T> ). int i = null; // won't compile - we all know this int? i = null; // will compile, and I'm glad it does, and it should compile, but why? How did Nullable<T> become an exception to the rule "You can't assign null to a value type?" The decompiled code for Nullable<T> offers no insights as of to how this

if condition with nullable

柔情痞子 提交于 2019-11-30 13:56:56
There is a lot of syntax sugar with Nullable<T> like those: int? parsed to Nullable<int> int? x = null if (x != null) // Parsed to if (x.HasValue) x = 56; // Parsed to x.Value = 56; And more. Why if condition with Nullable doesn't work? if (x) {} It gets Complier error saying can't convert Nullable<bool> to bool . Why it's not being parsed to if (x.HasValue && x.Value == true) or something similar? It's the most obvious usage for Nullable<bool> It's the most obvious usage for Nullable<bool> Your "obvious" behaviour leads to many inobvious behaviours. If if(x) is treated as false when x is null

Why is there a questionmark on the private variable definition?

我是研究僧i 提交于 2019-11-30 13:41:14
问题 I am reading an article about the MVVP Pattern and how to implement it with WPF. In the source code there are multiple lines where I cannot figure out what the question marks in it stand for. private DateTime? _value; What does the ? mean in the definition? I tried to find it in the help from VS but failed. 回答1: It's a nullable value. Structs, by default, cannot be nullable, they must have a value, so in C# 2.0, the Nullable<T> type was introduced to the .NET Framework. C# implements the

Generic type parameter and Nullable method overload

核能气质少年 提交于 2019-11-30 13:03:12
Hi I have this code using generic and nullable: // The first one is for class public static TResult With<TInput, TResult>(this TInput o, Func<TInput, TResult> evaluator) where TResult : class where TInput : class // The second one is for struct (Nullable) public static TResult With<TInput, TResult>(this Nullable<TInput> o, Func<TInput, TResult> evaluator) where TResult : class where TInput : struct Please note the TInput constraint, one is class, the other one is struct. Then I use them in: string s; int? i; // ... s.With(o => ""); i.With(o => ""); // Ambiguos method It cause an Ambiguos error

QuickWatch is not work correctly for show “.ToString()” of Nullable properties

余生长醉 提交于 2019-11-30 12:37:10
I have a nullable integer property in vb.net. This property in code has correct value, but in the QuickWatch always display 1 , unless I initial it by a value then display a six digitalis numbers. My codes is: Public Property MyNumber As Integer? MyNumber = 6546 MessageBox.Show(MyNumber.ToString()) And for nullable double property in QuickWatch always display 4/94065645841247E-324 . I test this on .Net 4 & 4.5 on visual studio 2010 & 2013 and get same result. However C# hasn't this problem EDIT: I append my project result too, as you see in watch windows both of them is shown Why this happens