null-coalescing

C# Null coalesce with LINQ

让人想犯罪 __ 提交于 2019-11-27 07:08:06
问题 I have 2 classes which looks like this: class Widget { string Selected { get; set; } List<Option> Options { get; set; } } class Option { string InternalCode { get; set; } string ExternalCode { get; set; } } Options gets populated dynamically with different data per client for showing ExternalCode as options Selected gets populated with ExternalCode . I then need to access the InternalCode which matches. At present I am doing this: var option = widget.Options.SingleOrDefault(o => o

Null Coalescing Operator in F#?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 19:52:54
问题 When interacting with C# libraries, I find myself wanting C#'s null coalescing operator both for Nullable structs and reference types. Is it possible to approximate this in F# with a single overloaded operator that inlines the appropriate if case? 回答1: Yes, using some minor hackery found in this SO answer "Overload operator in F#". At compiled time the correct overload for an usage of either ('a Nullable, 'a) ->'a or ('a when 'a:null, 'a) -> 'a for a single operator can be inlined. Even ('a

Is there a “null coalescing” operator in JavaScript?

自闭症网瘾萝莉.ら 提交于 2019-11-25 21:56:40
问题 Is there a null coalescing operator in Javascript? For example, in C#, I can do this: String someString = null; var whatIWant = someString ?? \"Cookies!\"; The best approximation I can figure out for Javascript is using the conditional operator: var someString = null; var whatIWant = someString ? someString : \'Cookies!\'; Which is sorta icky IMHO. Can I do better? 回答1: The JavaScript equivalent of the C# null coalescing operator ( ?? ) is using a logical OR ( || ): var whatIWant = someString