C# elegant way to check if a property’s property is null
In C# 6 you can use the Null Conditional Operator. So the original test will be: int? value = objectA?.PropertyA?.PropertyB?.PropertyC;
In C# 6 you can use the Null Conditional Operator. So the original test will be: int? value = objectA?.PropertyA?.PropertyB?.PropertyC;
I am using .NET 3.5 SP1 so it’s very simple: <TextBox Text=”{Binding Price, TargetNullValue=””}”/> Which stands for (thanks Gregor for your comment): <TextBox Text=”{Binding Price, TargetNullValue={x:Static sys:String.Empty}}”/> sys is the imported xml namespace for System in mscorlib: xmlns:sys=”clr-namespace:System;assembly=mscorlib” Hope that helped.
As Jason says, you can create a variable of the right type and pass that. You might want to encapsulate it in your own method: public static DateTime? TryParse(string text) { DateTime date; if (DateTime.TryParse(text, out date)) { return date; } else { return null; } } … or if you like the conditional operator: … Read more
The default value for int? — and for any nullable type that uses the “type?” declaration — is null. Why this is the case: int? is syntactic sugar for the type Nullable<T> (where T is int), a struct. (reference) The Nullable<T> type has a bool HasValue member, which when false, makes the Nullable<T> instance “act … Read more
System.String is a reference type and already “nullable”. Nullable<T> and the ? suffix are for value types such as Int32, Double, DateTime, etc.
If you are willing to make a runtime check in Foo’s constructor rather than having a compile-time check, you can check if the type is not a reference or nullable type, and throw an exception if that’s the case. I realise that only having a runtime check may be unacceptable, but just in case: public … Read more
The problem isn’t that null cannot be assigned to an int?. The problem is that both values returned by the ternary operator must be the same type, or one must be implicitly convertible to the other. In this case, null cannot be implicitly converted to int nor vice-versus, so an explict cast is necessary. Try … Read more
No, a nullable is a struct. What is happening is that the nullable struct has two values: The value of the data type (int for int?, DateTime for DateTime?, etc.). A boolean value which tells if the data type value has been set. (HasValue is the property.) When you set the value of the data … Read more
You are quite correct. Also in this question, the former solution is suggested while nobody actually notices ToString() already gives the correct answer. Maybe the argument for the more verbose solution is readability: When you call ToString() on something that is supposed to be null, you usually expect a NullReferenceException, although here it isn’t thrown.
You need to include a jar that this class exists in. You can find it here If using Maven, you can add the following dependency declaration: <dependency> <groupId>com.google.code.findbugs</groupId> <artifactId>jsr305</artifactId> <version>3.0.2</version> </dependency> and for Gradle: dependencies { testImplementation ‘com.google.code.findbugs:jsr305:3.0.2’ }