nullable

Assign value to Nullable<T> using FastMember

余生颓废 提交于 2019-12-10 10:05:29
问题 I have successfully assigned values to Properties and nested Properties using this function private static void AssignValueToProperty(ObjectAccessor accessor, object value, string propertyLambdaString) { var index = propertyLambdaString.IndexOf('.'); if (index == -1) { accessor[propertyLambdaString] = value; // problem above: throws Exception if assigning value to Nullable<T> } else { var property = propertyLambdaString.Substring(0, index); accessor = ObjectAccessor.Create(accessor[property])

nullable type and a ReSharper warning

≯℡__Kan透↙ 提交于 2019-12-10 04:13:18
问题 I have the following code: private static LogLevel? _logLevel = null; public static LogLevel LogLevel { get { if (!_logLevel.HasValue) { _logLevel = readLogLevelFromFile(); } return _logLevel.Value; } } private static LogLevel readLogLevelFromFile() { ... } I get a ReSharper warning on the return statement about a possible System.InvalidOperationException and it suggests I check _logLevel to see if it is null first. However, readLogLevelFromFile returns LogLevel , not LogLevel? , so there is

Boxing/Unboxing and Nullable?

主宰稳场 提交于 2019-12-10 03:45:50
问题 I understand that boxing and unboxing is about casting (real type to object... object to real type). But I do not understand what the MSDN say about it with the Nullable. Here is the text I do not understand: When a nullable type is boxed, the common language runtime automatically boxes the underlying value of the Nullable object, not the Nullable object itself. That is, if the HasValue property is true, the contents of the Value property is boxed. When the underlying value of a nullable type

Should References in Object-Oriented Programming Languages be Non-Nullable by Default? [closed]

ⅰ亾dé卋堺 提交于 2019-12-10 03:00:55
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Null pointers have been described as the "billion dollar mistake". Some languages have reference types which can't be assigned the

@Nullable and SonarQube 'Conditionally executed blocks should be reachable' warning

99封情书 提交于 2019-12-10 02:48:41
问题 Package has following package-info.java: @ParametersAreNonnullByDefault package foo; import javax.annotation.ParametersAreNonnullByDefault; Class has the following method: private static String toIsoString(@Nullable Instant dateTime) { return dateTime == null ? null : dateTime.toString(); } On which SonarQube (Version 6.2, SonarJava 4.14.0.11784) gives the following warning (squid:S2583): How can I convince SonarQube that the code is actually correct? Interestingly, SonarLint plugin (3.0.0

The '?' character cannot be used here

纵然是瞬间 提交于 2019-12-10 02:26:38
问题 With these variables: Dim d1 As Date? = Nothing Dim d2 As DateTime? = Nothing Dim i1 As Integer? = Nothing Dim i2 As Int32? = Nothing Why am I allowed to do this?: Dim flag1 As Boolean = Date?.Equals(d1, d2) Dim flag2 As Boolean = Integer?.Equals(i1, i2) ...but not allowed to do this?: Dim flag3 As Boolean = DateTime?.Equals(d2, d1) Dim flag4 As Boolean = Int32?.Equals(i2, i1) The last code will fail with an error saying: The '?' character cannot be used here. 回答1: VB.NET developers are not

Extension method for nullable enum

狂风中的少年 提交于 2019-12-10 02:06:34
问题 I'm trying to write an Extension method for nullable Enums. Like with this example: // ItemType is an enum ItemType? item; ... item.GetDescription(); So I wrote this method which doesn't compile for some reason that I don't understand: public static string GetDescription(this Enum? theEnum) { if (theEnum == null) return string.Empty; return GetDescriptionAttribute(theEnum); } I'm getting the following error on Enum? : only non-nullable value type could be underlying of system.nullable Why?

Creating a nullable<T> extension method ,how do you do it?

流过昼夜 提交于 2019-12-10 01:34:55
问题 I have a situation where I need to compare nullable types. Suppose you have 2 values: int? foo=null; int? bar=4; This will not work: if(foo>bar) The following works but obviously not for nullable as we restrict it to value types: public static bool IsLessThan<T>(this T leftValue, T rightValue) where T : struct, IComparable<T> { return leftValue.CompareTo(rightValue) == -1; } This works but it's not generic: public static bool IsLessThan(this int? leftValue, int? rightValue) { return Nullable

What is the use of Nullable<bool> type?

与世无争的帅哥 提交于 2019-12-10 01:16:20
问题 a bool variable could hold true or false, while bool? could be null as well. Why would we need a third value for bool ? If it is not true , what ever it is, it is == false Can you suggest a scenario where I would fancy a bool? instead. Thanks 回答1: Something can be true, false or undefined for many reasons. How would one answer "Is your third child a girl?" if one only has two children? Both true and false are incorrect. Null would be appropriate as saying that the comparison doesn't apply.

Nullable embedded value object with not nullable fields

≯℡__Kan透↙ 提交于 2019-12-09 07:31:39
问题 I created an entity "Person" in Doctrine2, and I added to it an Adress entity, which is a value object (embeddable). I want to allow a Person creation, without an Address, so I tag my embedded as "nullable = true". But on the other hand, my Address entity, if it exists, SHOULD contains at least some information (like city, zip code, etc...). So it has "nullable = false" attributes. Address: type: embeddable fields: [...] city: type: string length: 255 nullable: false Person: type: entity