nullable

Typescript type RequireSome<T, K extends keyof T> removing undefined AND null from properties

為{幸葍}努か 提交于 2021-02-20 02:59:29
问题 RequireSome type from another, very simialar question. This question is similar but it should not be a duplicate since here we want to also remove null as well as undefined from properties. Maybe the name shouldn't be Require but something like NonNullable or of this kind. The goal of this type is to specify which fields from a type should not be undefined or null, and return their types without undefined and null. type Question = { id: string; answer?: string | null; thirdProp?: number |

nullable bool in if() statement - checks required?

别来无恙 提交于 2021-02-19 01:08:52
问题 I just came across this in a coworkers code. He has a nullable bool type that he compares like this: //foo has no value here and evaluated to false if(foo==true) { doSomething(); } Typically, the way I check a nullable boolean is something like this: bool IsTrue(bool? value) { return value.HasValue && value.Value; } if(IsTrue(foo)){ doSomething() } Edit: I ran through both methods and they both appear work the same way. I'm asking which one is the correct way and if the extra checks are

Parsing value into nullable enumeration

末鹿安然 提交于 2021-02-18 22:24:29
问题 Let's say I have this: PriorityType? priority; string userInput = ...; I cannot change how this is defined: PriorityType? priority because it's actually part of a contract with another piece of code. I tried this, but it does not work: if (Enum.TryParse<PriorityType?>(userInput, out priority)) { What is the correct way? 回答1: The simplest way: PriorityType tempPriority; PriorityType? priority; if (Enum.TryParse<PriorityType>(userInput, out tempPriority)) priority = tempPriority; This is the

c#: assigning from nullable types

痞子三分冷 提交于 2021-02-16 13:46:06
问题 If I have a nullable "decimal? d" and I want to assign d to non nullable e, what is the proper way? 回答1: decimal e = d ?? 0.0; 回答2: decimal e; if(d.HasValue) { e = d.Value; } 回答3: You need to determine whether you even can, i.e. whether the nullable d has a value or not. if (d.HasValue) { e = d.Value; } else { /* now what */ } Another interesting case comes up quite commonly where you want to assign to a nullable using a ternary, in which case you have to cast to make both branches have the

Nullable Array and Why Do We Need Them

南楼画角 提交于 2021-02-11 14:05:45
问题 I see code like this and I understand it as making an array nullable but I dont understand why do we need it since arrays are ref types, so they are already nullable. So, my question is why do we need this? private readonly decimal?[] _amounts = new decimal?[_count]; 回答1: It's worth to mention that from C# 8.0 you can have a nullable reference type : https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/nullable-reference-types but as others mentioned this: private readonly decimal?[]

Convert array from nullable type to non-nullable of same type?

旧城冷巷雨未停 提交于 2021-02-08 15:26:50
问题 I would like to convert a Nullable(Of Byte)() array (a.k.a. byte?[] ) to a non-nullable array of the same type, that is, from byte?[] to byte[] . I'm looking for the simpler, easier, faster generic solution, in C# or VB.NET. I've found this generic function to convert between nullable types but I can't find a way to adapt the conversion logic to convert from a nullable type to a non-nullable type. This is a code example for which I feel the need to perform that kind of conversion: byte?[]

Same method signature confusion

倾然丶 夕夏残阳落幕 提交于 2021-02-08 07:26:07
问题 Assuming C# 8.0 and reference type nullability, why are these signatures considered equal: void Test<T>(IEnumerable<T> enumerable) where T : class void Test<T>(IEnumerable<T> enumerable) where T : struct but these are not? void Test<T>(IEnumerable<T?> enumerable) where T : class void Test<T>(IEnumerable<T?> enumerable) where T : struct The only thing different is the nullability of enumerable elements. 来源: https://stackoverflow.com/questions/60115233/same-method-signature-confusion

Same method signature confusion

。_饼干妹妹 提交于 2021-02-08 07:25:03
问题 Assuming C# 8.0 and reference type nullability, why are these signatures considered equal: void Test<T>(IEnumerable<T> enumerable) where T : class void Test<T>(IEnumerable<T> enumerable) where T : struct but these are not? void Test<T>(IEnumerable<T?> enumerable) where T : class void Test<T>(IEnumerable<T?> enumerable) where T : struct The only thing different is the nullability of enumerable elements. 来源: https://stackoverflow.com/questions/60115233/same-method-signature-confusion

Using C# 7.1 default literal in nullable optional argument causes unexpected behavior

只谈情不闲聊 提交于 2021-02-07 11:53:30
问题 C# 7.1 introduces a new feature called "Default Literals" that allows new default expressions. // instead of writing Foo x = default(Foo); // we can just write Foo x = default; For Nullable<T> types, the default value is null , and with the usual usage this works as expected: int? x = default(int?); // x is null int? x = default; // x is null However, when I try to use the new default literal as an optional argument (parameter) of a function, it's not working as expected: static void Foo(int?

Keep null when adding Nullable int?

走远了吗. 提交于 2021-02-05 10:44:26
问题 I want to add nullable int? and keep null when all values are null . I would like this results : 1 + 2 = 3 1 + null = 1 null + null = null O + null = 0 The problem is that if I sum a value with null, the result is null int? i1 = 1; int? i2 = null; int? total = i1 + i2; // null I have seen this thread : Is there a more elegant way to add nullable ints? With linq : var nums = new int?[] {null, null, null}; var total = nums.Sum(); // 0 I get 0 and I want null... The only way I have found is to