null-coalescing-operator

Null-coalescing operator and lambda expression

天涯浪子 提交于 2019-11-28 11:11:41
take a look at the following code I attempted to write inside a constructor: private Predicate<string> _isValid; //... Predicate<string> isValid = //...; this._isValid = isValid ?? s => true; The code doesn't compile - just "invalid expression term"s and so one. In contrast that does compile and I could just use it: this._isValid = isValid ?? new Predicate<string>(s => true); However, I still wonder why this syntax is not allowed. Any ideas? this._isValid = isValid ?? (s => true); Will work :) It parsed it this way: this._isValid = (isValid ?? s) => true; which does not make any sense. Check

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

倾然丶 夕夏残阳落幕 提交于 2019-11-28 10:47:58
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? 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 = default(T)) { var value = reader[column]; return (T)((DBNull.Value.Equals(value)) ? defaultValue : Convert

Understanding the null coalescing operator (??)

人盡茶涼 提交于 2019-11-28 03:14:53
问题 I have a custom WebControl which implements a .Value getter/setter returning a Nullable<decimal> It's a client-side filtered textbox (a subclass of TextBox with included javascript and some server side logic for setting/getting the value) Here is the getter & the setter from that control: public decimal? Value { get { decimal amount = 0; if (!decimal.TryParse(this.Text, NumberStyles.Currency, null, out amount)) { return null; } else { return amount; } } set { if (!value.HasValue) { this.Text

Possible to overload null-coalescing operator?

こ雲淡風輕ζ 提交于 2019-11-27 22:56:26
问题 Is it possible to overload the null-coalescing operator for a class in C#? Say for example I want to return a default value if an instance is null and return the instance if it's not. The code would look like something like this: return instance ?? new MyClass("Default"); But what if I would like to use the null-coalescing operator to also check if the MyClass.MyValue is set? 回答1: Good question! It's not listed one way or another in the list of overloadable and non-overloadable operators and

VB.NET null coalescing operator? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-11-27 21:45:59
Possible Duplicates: Coalesce operator and Conditional operator in VB.NET Is there a VB.NET equivalent for C#'s ?? operator? Is there a built-in VB.NET equivalent to the C# null coalescing operator? Cody Gray Yes, there is, a long as you're using VB 9 or later (included with Visual Studio 2008). You can use the version of the If operator overloaded to accept only two arguments: Dim myVar? As Integer = Nothing Console.WriteLine(If(myVar, 7)) More information can be found here in a blog post by the VB.NET team. (Yes, this is an operator , even though it looks like a function. It will compile

Null Coalescing Operator in F#?

▼魔方 西西 提交于 2019-11-27 19:24:46
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? 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 option, 'a) -> 'a can be thrown in for more flexibility. To provide closer behavior to c# operator, I've

Is there an “opposite” to the null coalescing operator? (…in any language?)

我是研究僧i 提交于 2019-11-27 18:14:17
null coalescing translates roughly to return x, unless it is null, in which case return y I often need return null if x is null, otherwise return x.y I can use return x == null ? null : x.y; Not bad, but that null in the middle always bothers me -- it seems superfluous. I'd prefer something like return x :: x.y; , where what follows the :: is evaluated only if what precedes it is not null . I see this as almost an opposite to null coalescence, kind of mixed in with a terse, inline null-check, but I'm [ almost ] certain that there is no such operator in C#. Are there other languages that have

C# null coalescing operator equivalent for c++

别来无恙 提交于 2019-11-27 17:19:31
问题 Is there a C++ equivalent for C# null coalescing operator? I am doing too many null checks in my code. So was looking for a way to reduce the amount of null code. 回答1: There isn't a way to do this by default in C++, but you could write one: in C# the ?? operator is defined as a ?? b === (a != null ? a : b) So, the C++ method would look like Coalesce(a, b) // put your own types in, or make a template { return a != null ? a : b; } 回答2: Using templates and C++11 lambdas. The first argument (left

Is there a more elegant way to add nullable ints?

。_饼干妹妹 提交于 2019-11-27 17:07:51
问题 I need to add numerous variables of type nullable int. I used the null coalescing operator to get it down to one variable per line, but I have a feeling there is a more concise way to do this, e.g. can't I chain these statements together somehow, I've seen that before in other code. using System; namespace TestNullInts { class Program { static void Main(string[] args) { int? sum1 = 1; int? sum2 = null; int? sum3 = 3; //int total = sum1 + sum2 + sum3; //int total = sum1.Value + sum2.Value +

C# ?? operator in Ruby?

孤街浪徒 提交于 2019-11-27 17:07:28
问题 Is it possible to implement the ?? operator in Ruby? a = nil b = 1 x = a ?? b # x should == 1 x = b ?? 2 # x should == 1 回答1: You're looking for conditional assignment: a ||= b # Assign if a isn't already set and the || operator a = b || 2 # Assign if b is assigned, or assign 2 回答2: In Ruby, the short-circuiting Boolean operators ( || , && , and and or ) do not return true or false , but rather the first operand that determines the outcome of the entire expression. This works, because Ruby