null-coalescing-operator

Null coalesce operator with casting

喜你入骨 提交于 2019-12-01 19:27:41
I have upgraded to PHP 7 and started using the null coalesce operator to convert things like $email = isset($_SESSION['email']) ? $_SESSION['email'] : ''; to $email = $_SESSION['email'] ?? ''; but I can't figure out how to do this if I am casting as well. For example, the for the old statement $id = isset($_GET['id']) ? (int) $_GET['id'] : 0; I would think something like $id = (int) $_GET['id'] ?? 0; should work, but it doesn't appear to in the sense that, if $_GET['id'] is not set, $id resolves to 0 but I get the Notice Notice: Undefined index: id in test.php on line 2 The (int) cast gets

Does null coalescing operator cache the result in c#

一世执手 提交于 2019-12-01 15:36:39
I know that doing (myValue ?? new SomeClass()) is similar to (myValue == null ? new SomeClass() : myValue) But out of curiosity, is there any performance benefit when I call a function, say (getResult() ?? new SomeClass()) . Will getResult() get executed twice? It seems unintuitive since I've specified the method call only once. Well, if by "caching" you mean storing it in a temporary variable, then yes. This construct: var result = (getResult() ?? new SomeClass()); can be thought of to be equivalent to this: var <temp> = getResult(); if (<temp> == null) <temp> = new SomeClass(); result =

using coalescing null operator on nullable types changes implicit type

巧了我就是萌 提交于 2019-12-01 15:12:29
I would expect the next three lines of code to be the same: public static void TestVarCoalescing(DateTime? nullableDateTime) { var dateTimeNullable1 = nullableDateTime.HasValue ? nullableDateTime : DateTime.Now; var dateTimeNullable2 = nullableDateTime != null ? nullableDateTime : DateTime.Now; var dateTimeWhatType = nullableDateTime ?? DateTime.Now; } In all cases, I assign nullableDateTime to the new variable. I would expect the type of all variables to become DateTime? since that is the type of nullableDateTime . But to my surprise, the type of dateTimeWhatType just becomes DateTime , so

using coalescing null operator on nullable types changes implicit type

时间秒杀一切 提交于 2019-12-01 14:00:25
问题 I would expect the next three lines of code to be the same: public static void TestVarCoalescing(DateTime? nullableDateTime) { var dateTimeNullable1 = nullableDateTime.HasValue ? nullableDateTime : DateTime.Now; var dateTimeNullable2 = nullableDateTime != null ? nullableDateTime : DateTime.Now; var dateTimeWhatType = nullableDateTime ?? DateTime.Now; } In all cases, I assign nullableDateTime to the new variable. I would expect the type of all variables to become DateTime? since that is the

Null coalescing within an invocation chain

偶尔善良 提交于 2019-12-01 11:27:11
If I have a long list of objects that each has the possibility of returning null within a "Linq where" clause, e.g. SomeSource.Where(srcItem=>(srcItem.DataMembers["SomeText"].Connection.ConnectedTo as Type1).Handler.ForceInvocation == true)); the indexer can return null and the "as" operator may return null. It is possible that the object does not have a connection (ie. The property is null). If a null is encountered anywhere, I would like the where clause to return "false" for the item being evaluated. Instead, it aborts with a null reference exception. It appears to me that this would be

Null coalescing within an invocation chain

坚强是说给别人听的谎言 提交于 2019-12-01 09:24:06
问题 If I have a long list of objects that each has the possibility of returning null within a "Linq where" clause, e.g. SomeSource.Where(srcItem=>(srcItem.DataMembers["SomeText"].Connection.ConnectedTo as Type1).Handler.ForceInvocation == true)); the indexer can return null and the "as" operator may return null. It is possible that the object does not have a connection (ie. The property is null). If a null is encountered anywhere, I would like the where clause to return "false" for the item being

Is it possible to use operator ?? and throw new Exception()?

本秂侑毒 提交于 2019-12-01 02:07:36
I have a number of methods doing next: var result = command.ExecuteScalar() as Int32?; if(result.HasValue) { return result.Value; } else { throw new Exception(); // just an example, in my code I throw my own exception } I wish I could use operator ?? like this: return command.ExecuteScalar() as Int32? ?? throw new Exception(); but it generates a compilation error. Is it possible to rewrite my code or there is only one way to do that? For C# 7 In C# 7, throw becomes an expression, so it's fine to use exactly the code described in the question. For C# 6 and earlier You can't do that directly in

Is it possible to use operator ?? and throw new Exception()?

别说谁变了你拦得住时间么 提交于 2019-11-30 21:31:20
问题 I have a number of methods doing next: var result = command.ExecuteScalar() as Int32?; if(result.HasValue) { return result.Value; } else { throw new Exception(); // just an example, in my code I throw my own exception } I wish I could use operator ?? like this: return command.ExecuteScalar() as Int32? ?? throw new Exception(); but it generates a compilation error. Is it possible to rewrite my code or there is only one way to do that? 回答1: For C# 7 In C# 7, throw becomes an expression, so it's

Is it possible to coalesce string and DBNull in C#?

我的未来我决定 提交于 2019-11-30 14:57:01
问题 I'm writing a C# routine to call a stored proc. In the parameter list I'm passing in, it is possible that one of the values can legally be null. So I thought I'd use a line like this: cmd.Parameters.Add(new SqlParameter("@theParam", theParam ?? DBNull.Value)); Unfortunately, this returns the following error: CS0019: Operator '??' cannot be applied to operands of type 'string' and 'System.DBNull' Now, this seems clear enough, but I don't understand the rationale behind it. Why would this not

Is it possible to coalesce string and DBNull in C#?

心不动则不痛 提交于 2019-11-30 12:44:05
I'm writing a C# routine to call a stored proc. In the parameter list I'm passing in, it is possible that one of the values can legally be null. So I thought I'd use a line like this: cmd.Parameters.Add(new SqlParameter("@theParam", theParam ?? DBNull.Value)); Unfortunately, this returns the following error: CS0019: Operator '??' cannot be applied to operands of type 'string' and 'System.DBNull' Now, this seems clear enough, but I don't understand the rationale behind it. Why would this not work? (And often, when I don't understand why something isn't working, it's not that it can't work...it