nullable

How to create a Nullable<T> from a Type variable?

江枫思渺然 提交于 2019-12-10 17:54:18
问题 I'm working with expressions and I need a method which receives an object of some type (currently unknown). Something like this: public static void Foobar(object Meh) { } What I need to is make this method return a Nullable<T> version of Meh , but the type T is from Meh.GetType() . So the return would be Nullable<MehType> , where MehType is the type of Meh . Any ideas or suggestions? Thanks Update: the reason why I needed this is because of this exception: The binary operator Equal is not

Why aren't F# records allowed to have AllowNullLiteralAttribute?

早过忘川 提交于 2019-12-10 17:37:05
问题 Is there a compiler implementation reason why records can't have the AllowNullLiteralAttribute attribute or is this a chosen constraint? I do see this constraint force cleaner code sometimes but not always. [<AllowNullLiteralAttribute>] type IBTreeNode = { mutable left : IBTreeNode; mutable right : IBTreeNode; mutable value : int} with member this.Insert x = if x < this.value then if this.left = null then this.left <- {left = null; right = null; value = x} else this.left.Insert x else if this

What's causing “Invalid index nn for this SqlParameterCollection with Count=nn” when a column is Null in the database?

浪尽此生 提交于 2019-12-10 16:17:08
问题 For Accommodation entity we have two columns that are nullable: CollectionType and AccommodationUnitType . However I noticed in the data that they were set to zero rather than null, causing NHibernate to try and find an entity with id 0. This was a lot of extra unnecessary DB calls, so I updated the relevant data to NULL in the database, and suddenly I get a big fat error: "Invalid index 24 for this SqlParameterCollection with Count=24" Here is the mapping override I'm using: public void

TryXXX-like methods with “out” parameters vs returning a nullable value type?

守給你的承諾、 提交于 2019-12-10 15:34:53
问题 I often see methods like this in C#: (doing a computation that may or may not give a result) bool TrySomething(SomeType inputData, out SomeOtherType result) { ... } Why people don't use instead something like this? Nullable<SomeOtherType> TrySomething(SomeType inputData) { ... } Is it just performance difference? This is a struct , so there must be no heap allocation, right? Or I have missed something? 回答1: Nullable was introduced - like generics - in C# 2.0. There is a lot of code that

VB.Net Linq to Entities Null Comparison - 'Is Nothing' or '= Nothing'?

非 Y 不嫁゛ 提交于 2019-12-10 14:56:47
问题 We have several projects in VB.Net, using .Net Framework 4 and Linq to Entities for many of our SQL queries. Moving to EF is a new shift for us (been using it for about 4-6 months) and has the backing of upper management because we can code so much faster. We still use a lot of stored procs, but we even execute those through Linq to Entities as well. I'm hoping to clear some confusion up and I can't find a direct answer that makes sense. We have some queries where we want records where a

Cannot change type to nullable in generic method

一个人想着一个人 提交于 2019-12-10 14:47:33
问题 I am creating a generic converter Here is a sample code of the generic converter bool TryReaderParse<TType>(object data, out TType value) { value = default(TType); Type returnType = typeof(TType); object tmpValue = null; if (returnType == typeof(DateTime)) { tmpValue = StringToDatetime(data.ToString()); } else if (returnType == typeof(DateTime?)) // THIS IF FIRES { tmpValue = StringToNullableDatetime(data.ToString()); } value = (TType)Convert.ChangeType(tmpValue, returnType); // THROWS }

Is there any way to combine these almost identical classes into one?

那年仲夏 提交于 2019-12-10 14:34:10
问题 Followup to this question: Why is Nullable<T> considered a struct and not a class? I have two classes that essentially maintain a tuple of some user-supplied value with an internal object. When the type of the user-supplied value is a primitive, I have to wrap it in a Nullable<T> so that it can take on a null value in the Tuple. public class BundledClass<T> where T : class { private Tuple<T, object> _bundle; public T Value { get { return _bundle == null ? null : _bundle.Item1; } set { _bundle

How can I make IntelliJ IDEA understand my null-checking method?

跟風遠走 提交于 2019-12-10 13:15:56
问题 I have a method where a parameter is marked with the @Nonnull annotation. The code which calls the method has to check whether the value is null. Rather than just a straight x != null check, it is calling a utility method on another class. (In the real code, the utility method also checks whether it is a blank String). My problem is that Intellij Idea is showing an inspection warning on the Nonnull method call, saying that my variable "might be null". I know it cannot be null because of the

Take the greater of two nullable values

房东的猫 提交于 2019-12-10 12:28:51
问题 Suppose I have two nullable integers: int? a = 10; int? b = 20; I want to take the biggest, non-null value, such that if both values are null, the result is null. I could write something long-winded such as: int? max; if (a == null) { max = b; } else if (b == null) { max = a; } else { max = a > b ? a : b; } This feels a little too clunky (and possibly error-prone) for my liking. What's the simplest way to return the greater value, which also accounts for the possibility of null values? 回答1:

Nullable<int?> is not possible, Why not? [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-12-10 10:31:22
问题 This question already has answers here : Why can't I write Nullable<Nullable<int>>? (5 answers) Closed 5 years ago . Excuse me if its a silly question, I am trying to get a better understanding of Nullable types in .Net. From what i notice from Microsoft source code (using ReSharper), I understand that Nullable is a struct and T needs to be a struct public struct Nullable<T> where T : struct Now, I tried to do something like this public struct CustomNullable<T> where T : struct { } public