nullable

Is there a way to use the default value on a non-optional parameter when null is passed?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 15:09:10
问题 For example, if I have the following data class: data class Data( val name: String = "", val number: Long = 0 ) And functions that can return null : fun newName(): String? {} fun newNumber(): Long? {} I know I can use the following to use the value of the functions if they are not null : val newName = newName() val newNumber = newNumber() val data = Data( if (newName != null) newName else "", if (newNumber != null) newNumber else 0 ) But is there a way to just use the default value specified

Trying to understand ?. (null-conditional) operator in C#

倖福魔咒の 提交于 2019-12-04 14:55:48
问题 I have this very simple example: class Program { class A { public bool B; } static void Main() { System.Collections.ArrayList list = null; if (list?.Count > 0) { System.Console.WriteLine("Contains elements"); } A a = null; if (a?.B) { System.Console.WriteLine("Is initialized"); } } } The line if (list?.Count > 0) compiles perfectly which means that if list is null , the expression Count > 0 becomes false by default. However, the line if (a?.B) throws a compiler error saying I can't implicitly

How to use Objective-C __nonnull in a backwards-compatible way?

江枫思渺然 提交于 2019-12-04 10:00:53
Xcode has recently added __nonnull , __nullable , etc. attributes. However, they're not supported by older versions of clang and other compilers. How can I use these attributes in a compatible way? I hoped something like this would work: #ifndef NS_ASSUME_NONNULL_BEGIN #define __nonnull #endif but it seems that NS_ASSUME_NONNULL_BEGIN is not a real macro, and it's "not defined" in Xcode7. And it would make sense for this to work: #if !defined(__is_identifier) || __is_identifier(__nonnull) #define __nonnull #define __nullable #endif but Xcode 6 chokes on that with "token is not a valid binary

Is Nullable<int> a “Predefined value type” - Or how does Equals() and == work here?

梦想的初衷 提交于 2019-12-04 09:57:25
问题 For my own implementation of an Equals() method, I want to check a bunch of internal fields. I do it like this: ... _myNullableInt == obj._myNullableInt && _myString == obj._myString && ... I would assume, that this compares the values, including null, for equality not the object address (as a reference euqality compare operation would) because: It is said so for "predefined value types" in this MSDN doc here. I assume Nullable<int> is such a "predefined value type" because of it is in the

How is Nullable<T> different from a similar custom C# struct?

不打扰是莪最后的温柔 提交于 2019-12-04 09:54:07
In Nullable micro-optimizations, part one , Eric mentions that Nullable<T> has a strange boxing behaviour that could not be achieved by a similar user-defined type. What are the special features that the C# language grants to the predefined Nullable<T> type? Especially the ones that could not be made to work on a MyNullable type. Of course, Nullable<T> has special syntactic sugar T? , but my question is more about semantics. Eric Lippert What I was getting at is: there is no such thing as a boxed nullable . When you box an int , you get a reference to a boxed int . When you box an int? , you

How to correctly deal with System.Nullable<T> fields of LinqToSql classes?

喜你入骨 提交于 2019-12-04 08:13:38
I've got a table with some nullable double fields. Working with LinqToSQL trying to use the field directly I get Argument type System.Nullable is not assignable to parameter type double How do I deal with this correctly? The problem has nothing to do with Linq. It's got to do with conversion between double and double? / Nullable<double> . Implicit conversion from double? to double is not allowed: double? foo ; double bar = foo ; You can reference the double value directly: double? foo ; double bar = foo.Value ; This will throw a NullReferenceException if the Nullable<T> is null (that is,

Do short-circuiting operators || and && exist for nullable booleans? The RuntimeBinder sometimes thinks so

孤街醉人 提交于 2019-12-04 07:29:40
问题 I read the C# Language Specification on the Conditional logical operators || and && , also known as the short-circuiting logical operators. To me it seemed unclear if these existed for nullable booleans, i.e. the operand type Nullable<bool> (also written bool? ), so I tried it with non-dynamic typing: bool a = true; bool? b = null; bool? xxxx = b || a; // compile-time error, || can't be applied to these types That seemed to settle the question (I could not understand the specification clearly

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

我们两清 提交于 2019-12-04 06:58:12
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 { Male, Female } Case I: int? i = 1; object o = i; Sex e = (Sex)o; //success //but Sex e = Sex.Male; object

InvalidCastException trying to cast from a boxed int to a nullable enum

ぐ巨炮叔叔 提交于 2019-12-04 03:28:28
问题 I have an enum, Foo : public enum Foo { Alpha, Bravo, Charlie } If I attempt the following cast from a boxed int to a Foo? , I get an InvalidCastException : var x = (Foo?)(object)1; This led me to some experimentation... var x = (Foo)(object)1; // succeeds var x = (long)(object)1; // fails var x = (long?)(object)1; // fails var x = (long)1; // succeeds var x = (long?)1; // succeeds var x = (int)(object)1; // succeeds var x = (int?)(object)1; // succeeds What this tells me is that you can cast

Bind nullable DateTime to MaskedTextBox

Deadly 提交于 2019-12-04 03:22:54
问题 I have a masked text box bound to a nullabe datetime, but when the date is blanked out, the validation on the masked text box won't complete. Is there a way to force this behaviour? I want a blanked out text box to equal a null DateTime. When the textbox is already null, the validation works. It only breaks when there is a date already bound and I try to blank it out. 回答1: I figured out it didn't have to do with the validation. It was when the date was being parsed back to the datetime. This