null-coalescing-operator

Possible to use ?? (the coalesce operator) with DBNull?

半腔热情 提交于 2019-11-27 03:47:37
问题 If I have code similar to the following: while(myDataReader.Read()) { myObject.intVal = Convert.ToInt32(myDataReader["mycolumn"] ?? 0); } It throws the error: Object cannot be cast from DBNull to other types. defining intVal as a nullable int is not an option. Is there a way for me to do the above? 回答1: Can you use an extension method? (written off the top of my head) public static class DataReaderExtensions { public static T Read<T>(this SqlDataReader reader, string column, T defaultValue =

C#'s null coalescing operator (??) in PHP

て烟熏妆下的殇ゞ 提交于 2019-11-27 01:23:28
Is there a ternary operator or the like in PHP that acts like ?? of C#? ?? in C# is clean and shorter, but in PHP you have to do something like: // This is absolutely okay except that $_REQUEST['test'] is kind of redundant. echo isset($_REQUEST['test'])? $_REQUEST['test'] : 'hi'; // This is perfect! Shorter and cleaner, but only in this situation. echo null? : 'replacement if empty'; // This line gives error when $_REQUEST['test'] is NOT set. echo $_REQUEST['test']?: 'hi'; PHP 7 adds the null coalesce operator : // Fetches the value of $_GET['user'] and returns 'nobody' // if it does not exist

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 Python equivalent of the C# null-coalescing operator?

久未见 提交于 2019-11-26 19:44:06
In C# there's a null-coalescing operator (written as ?? ) that allows for easy (short) null checking during assignment: string s = null; var other = s ?? "some default value"; Is there a python equivalent? I know that I can do: s = None other = s if s else "some default value" But is there an even shorter way (where I don't need to repeat s )? Juliano other = s or "some default value" Ok, it must be clarified how the or operator works. It is a boolean operator, so it works in a boolean context. If the values are not boolean, they are converted to boolean for the purposes of the operator. Note

?? Null Coalescing Operator --> What does coalescing mean?

久未见 提交于 2019-11-26 17:48:46
问题 I'm tempted to lie and say that English is my second language, but the truth is that I just have no idea what 'Coalescing' means. I know what ?? 'does' in C#, but the name doesn't make sense to me. I looked up the word and I understand it to be a synonym for 'join'. 'Null Join Operator' still doesn't make sense. Can someone enlighten me? 回答1: I'm tempted to lie and say that English is my second language...but the truth is that I just have no idea what 'Coalescing' means. I know what ?? 'does'

Weird operator precedence with ?? (null coalescing operator)

无人久伴 提交于 2019-11-26 17:47:53
问题 Recently I had a weird bug where I was concatenating a string with an int? and then adding another string after that. My code was basically the equivalent of this: int? x=10; string s = "foo" + x ?? 0 + "bar"; Amazingly enough this will run and compile without warnings or incompatible type errors, as will this: int? x=10; string s = "foo" + x ?? "0" + "bar"; And then this results in an unexpected type incompatibility error: int? x=10; string s = "foo" + x ?? 0 + 12; As will this simpler

Null coalescing in powershell

此生再无相见时 提交于 2019-11-26 17:35:42
问题 Is there a null coalescing operator in powershell? I'd like to be able to do these c# commands in powershell: var s = myval ?? "new value"; var x = myval == null ? "" : otherval; 回答1: No need for the Powershell Community Extensions, you can use the standard Powershell if statements as an expression: variable = if (condition) { expr1 } else { expr2 } So to the replacements for your first C# expression of: var s = myval ?? "new value"; becomes one of the following (depending on preference): $s

Coalesce function for PHP?

别说谁变了你拦得住时间么 提交于 2019-11-26 17:13:58
Many programming languages have a coalesce function (returns the first non-NULL value, example ). PHP, sadly in 2009, does not. What would be a good way to implement one in PHP until PHP itself gets a coalesce function? There is a new operator in php 5.3 which does this: ?: // A echo 'A' ?: 'B'; // B echo '' ?: 'B'; // B echo false ?: 'B'; // B echo null ?: 'B'; Source: http://www.php.net/ChangeLog-5.php#5.3.0 PHP 7 introduced a real coalesce operator : echo $_GET['doesNotExist'] ?? 'fallback'; // prints 'fallback' If the value before the ?? does not exists or is null the value after the ?? is

Is there a VB.NET equivalent for C#&#39;s &#39;??&#39; operator?

蹲街弑〆低调 提交于 2019-11-26 11:49:19
Is there a VB.NET equivalent for C#'s ?? operator? Firas Assaad Use the If() operator with two arguments ( Microsoft documentation ): ' Variable first is a nullable type. Dim first? As Integer = 3 Dim second As Integer = 6 ' Variable first <> Nothing, so its value, 3, is returned. Console.WriteLine(If(first, second)) second = Nothing ' Variable first <> Nothing, so the value of first is returned again. Console.WriteLine(If(first, second)) first = Nothing second = 6 ' Variable first = Nothing, so 6 is returned. Console.WriteLine(If(first, second)) Nick The IF() operator should do the trick for

an expression tree lambda may not contain a null propagating operator

我们两清 提交于 2019-11-26 11:19:31
问题 Question : The line price = co?.price ?? 0, in the following code gives me the above error. but if I remove ? from co.? it works fine. I was trying to follow this MSDN example where they are using ? on line select new { person.FirstName, PetName = subpet?.Name ?? String.Empty }; So, it seems I need to understand when to use ? with ?? and when not to. Error : an expression tree lambda may not contain a null propagating operator public class CustomerOrdersModelView { public string CustomerID {