What's the best implementation of safe navigation operator - c#

Is there an operator in C# that behaves like the safe navigation operator in groovy?
For instance, in groovy, doing something like this will prevent it from getting a NullPointerException if SessionData.CurrentSeminar is null.
int respId = SessionData.CurrentSeminar?.SeminCbaRespId;
How is this accomplished with C#?

That operator does not exist in C#. You could do it with an inline-if
int respId = SessionData.CurrentSeminar != null ?
SessionData.CurrentSeminar.SeminCbaRespId : default(int);
or as an extension method.
var respId = SessionData.CurrentSeminar.GetSeminCbaRespId();
public static int GetSeminCbaRespId(this typeofCurrentSeminar CurrentSeminar)
{
return CurrentSeminar != null ? CurrentSeminar.SeminCbaRespId : default(int);
}

Maybe workaround like this?
int respId= ReferenceEquals(SessionData.CurrentSeminar,null)?-1:SessionData.CurrentSeminar.SeminCbaRespId;

The closest operator is ?:, but it's not as sugary.
So, you could do:
int respId = SessionData.CurrentSeminar != null ? SessionData.CurrentSeminar.SeminCbaRespId : 0; // if 0 is the "null" value

There is no operator for this, but you can get close. Try the one from this answer:
int respId = SessionData.CurrentSeminar.NullOr(s => s.SeminCbaRespId) ?? 0;
This gets extra useful if you need to chain several of them:
var elem = xml.Element("abc")
.NullOr(x => x.Element("def"))
.NullOr(x => x.Element("blah");

Related

can I do an assignment in C# with a shorter conditional statement

I would like to know if there is simpler way to check a condition and assign the same variable if the condition is false similar to null-coalescing operator (??)
Here is how it can be done now
int i = 0;
...
int j = (i == 0) ? 1 : i;
Can it be made simpler like
int i = 0
...
int j = (i == 0) ?? 1;
I understand it won't work that way since ?? only checks for null. But I'm wondering if it can achieved with a different operator.
You could create an extension method for int types that will do what you want:
public static class Extensions
{
public static int IfZero (this int value, int defaultValue)
{
return value == 0 ? defaultValue : value;
}
public static int ReplaceValue(this int value, int valueToReplace, int replaceWith)
{
return value == valueToReplace ? replaceWith : value;
}
}
Then it could be used throughout your program like:
int i = 0;
int j = i.IfZero(1);
// Or using the more generic method:
int j = i.ReplaceValue(0, 1);
I think what you're looking for is impossible in general (as opposed to when more info is available as I mention below). The reason is that you need 3 pieces of information for the job:
the value to check for
the value to use if true
the value to use if false
The ?? operator gets around that by assuming that 1 is null. But that would mean that you would need a separate operator for every value of 1 that you would want to test for. This is worthwhile for the C# team to write for something as common as null. Not for most things. (Of courses you could write your own for cases you know you need. But it seems like you're looking for something built in.)
In your specific case
Assuming that the minimum has to be 1, you can have:
j = Math.Max(1, i);
In response to those asking why would the OP be looking for simpler code than already very simple code, this might be to reflect the logic behind the action, and therefore be (in a sense) more readable.
No, the language does not offer that.
You can write a generic extension method, though:
public static class CoalescingExtensions
{
public static T IfDefault<T> (this T value, T valueIfDefault)
{
return value.Equals(default(T)) ? valueIfDefault: value;
}
}
The code is using 0 to determine whether or not the variable has been initialized. However, this can create issues since 0 is a valid value for an integer.
If checking for a different default value is acceptable, try using nullable types. This will allow you to use the null coalescing operator.
int? i = null;
...
int j = i?? 1;
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/nullable-types/

What is the fastest way to check for nullable bool being true in C#?

Wondering if there is a simpler way to check for a nullable bool being true.
I find myself doing code like this a lot which gets real bulky. Any faster way to do it?
bool? x = false;
if (x.hasValue && x.Value) ...
Seems like there must be a clean faster way to check for true
Use GetValueOrDefault:
if(x.GetValueOrDefault(false))
You can also use this with other types.
if (x == true)
That should work and is the shortest
May be many developers are not familiar with this but you can use the null coalesce operator (??), as shown below:
int? x = null;
// Set y to the value of x if x is NOT null; otherwise,
// if x = null, set y to -1.
int y = x ?? -1;
and for condition check :-
if (nullableBool ?? false) { ... }
and another option is GetValueOrDefault Method
if (nullableBool.GetValueOrDefault(false))
{
}

Is there a conditional operator for setting a variable in C# to the value of another variable if the latter isn't null?

Something like the ternary operator (?:) or the null coalescing operator (??). It seems silly to me to take up two lines and be so wordy when the other operators exist.
EDIT:
Since it's requested, here's two possible examples of what I hope that I can find
var variable ?= mightBeNull;
or
var variable = mightBeNull != null ? mightBeNull
Really, it's either something that can be used to assign a value, if it's not null, to a variable or an If without an Else packaged nicely in an operator.
The ??= operator is coming to C# 8.
int? i = null; // i = null
i ??= 0; // i = 0
i ??= 1; // i = 0
// different ways of writing 'i ??= 0;':
i = i ?? 0;
if (i == null) i = 0;
So you want this?
if (other != null)
someVariable = other;
You could do the following, but I'd argue that the above is better due to clarity and possible side effects:
someVariable = other ?? someVariable;
The above might cause side effects if someVariable is a property and either the get or set can cause side effects; this shouldn't be important if your property follows the ordinary guidelines. Or, as Servy points out, even if it's a field, it could created different semantics in a multithreaded app. I should note that in the first example, you read other twice, which also has the potential for complexity to enter (though a smaller potential than the latter example).
I bet this is what you are looking for.
Let's say we have a function, myFunction that says whether the argument passed input is null or not. If input is null, it will return "Input is null!", else it will return "Input is not null".
This is the normal approach:
public String myFunction(string input) {
if (input == null)
return "Input is null!";
else
return "Input is not null";
}
And this is the other approach (What you are looking for):
public String myFunction(string input) {
return (input == null) ? "Input is null!" : "Input is not null";
}
It is called (?: conditional operator).
To assign a value to a variable only if it is not null, you would need to use an if. Neither the conditinal nor the null coalesce operators would do that.
if(somethingElse != null) something = somethingElse;
There is no specific operator in C# that does exactly this.
In Visual Studio 2015 C# there is a new operator called the Null-Conditional that does what you are asking. It is the ?. or ? operator.
int? length = customers?.Length; // null if customers is null
Customer first = customers?[0]; // null if customers is null
int? count = customers?[0]?.Orders?.Count(); // null if customers, the first customer, or Orders is null
Currently useable in previous versions of C# but not an operator but rather a one liner would be to use the ? : statement
?: operator
condition ? first_expression : second_expression;
AssingningTO = (someVar == null) ? null: someVar; // if null assign null ELSE assign someVar
AssingningTO = (someVar == null) ? String.Empth(): someVar; // if null assign empty string else someVar
Nullable types can represent all the values of an underlying type, and an additional null value.
http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx

Convert null field to zero before converting to int?

In my program, I'm looping through a datatable to get data from each field. One line of my code looks like this:
int LYSMKWh = Convert.ToInt32(resultsDT.Rows[currentRow]["LYSMKWh"]);
Basically, I'm just taking the value that's in the cell and converting it to an int32. I run into a problem, though, when the data in the field is a null. I get an Invalid Cast error message about not being able to convert a "DBNull" to another type.
So, I know that I can just check the value of the field before I try to convert it by doing something like this:
if (resultsDT.Rows[currentRow]["LYSMKWh"] == null)
{
resultsDT.Rows[currentRow]["LYSMKWh"] = 0;
}
But, is there a better way to do this? Is there something I can do "inline" while I'm trying to convert the value without having to resort to using the if ?
EDIT: I did try using this suggested method:
int LYSMKWh = Convert.ToInt32(resultsDT.Rows[currentRow]["LYSMKWh"] ?? "0");
Unfortunately, I still received the Invalid Cast error message with regards to the DBNull type. It was suggested that I could have problems using the ?? operator if the types on either side were different.
Also, since I have complete control over the data that I'm building my datatable with, I changed it so that no null values would be written to any field. So, that pretty much negates the need to convert a null to an int32, in this case. Thanks for all the advice, everyone!
You could do this:
var LYSMKWh =
resultsDT.Rows[currentRow]["LYSMKWh"].Equals(DBNull.Value)
? 0
: Convert.ToInt32(resultsDT.Rows[currentRow]["LYSMKWh"]);
use the ?? operator:
resultsDT.Rows[currentRow][...] ?? "0"
(expecting the field to be a string - if not change the "0")
You may consider using C#'s ?? operator, which checks a value for null and if it's null, assigns a default value to it.
You can use the ?? operator:
object x = null;
int i = (int)(x ?? 0)
You can use a custom convert method:
public static int ConvertToInt32(object value, int defaultValue) {
if (value == null)
return defaultValue;
return Convert.ToInt32(value);
}
You may need overloads that take other types, like string. You may have problems with the ?? operator if the types on either side are different.
You can replace it with a ternary operator:
var rowData = resultsDT.Rows[currentRow]["LYSMKWh"];
int LYSMKWh = rowData != null ? Convert.ToInt32(rowData) : 0;
Alternatively, the ?? operator could work:
int LYSMKWh = Convert.ToInt32(resultsDT.Rows[currentRow]["LYSMKWh"] ?? 0);
But I think that's less readable.
You can use extension Method.
int LYSMKWh = resultsDT.Rows[currentRow]["LYSMKWh"].IfNullThenZero();
Create the below class
public static class Converter
{
public static Int32 IfNullThenZero(this object value)
{
if (value == DBNull.Value)
{
return 0;
}
else
{
return Convert.ToInt32(value);
}
}
}
Check if it's DBNull.Value instead of null:
if (resultsDT.Rows[currentRow]["LYSMKWh"] == DBNull.Value)
{
resultsDT.Rows[currentRow]["LYSMKWh"] = 0;
}
The ternary expression would work like this:
var LYSMKwhField = resultsDT.Rows[currentRow]["LYSMKWh"];
int LYSMKWh = LYSMKwhField != DBNull.Value ? Convert.ToInt32(rowData) : 0;

Shortest way to check for null and assign another value if not

I am pulling varchar values out of a DB and want to set the string I am assigning them to as "" if they are null. I'm currently doing it like this:
if (string.IsNullOrEmpty(planRec.approved_by) == true)
this.approved_by = "";
else
this.approved_by = planRec.approved_by.toString();
There seems like there should be a way to do this in a single line something like:
this.approved_by = "" || planRec.approved_by.toString();
However I can't find an optimal way to do this. Is there a better way or is what I have the best way to do it?
Try this:
this.approved_by = IsNullOrEmpty(planRec.approved_by) ? "" : planRec.approved_by.toString();
You can also use the null-coalescing operator as other have said - since no one has given an example that works with your code here is one:
this.approved_by = planRec.approved_by ?? planRec.approved_by.toString();
But this example only works since a possible value for this.approved_by is the same as one of the potential values that you wish to set it to. For all other cases you will need to use the conditional operator as I showed in my first example.
Starting with C# 8.0, you can use the ??= operator to replace the code of the form
if (variable is null)
{
variable = expression;
}
with the following code:
variable ??= expression;
More information is here
You are looking for the C# coalesce operator: ??. This operator takes a left and right argument. If the left hand side of the operator is null or a nullable with no value it will return the right argument. Otherwise it will return the left.
var x = somePossiblyNullValue ?? valueIfNull;
The coalesce operator (??) is what you want, I believe.
My guess is the best you can come up with is
this.approved_by = IsNullOrEmpty(planRec.approved_by) ? string.Empty
: planRec.approved_by.ToString();
Of course since you're hinting at the fact that approved_by is an object (which cannot equal ""), this would be rewritten as
this.approved_by = (planRec.approved_by ?? string.Empty).ToString();
With C#6 there is a slightly shorter way for the case where planRec.approved_by is not a string:
this.approved_by = planRec.approved_by?.ToString() ?? "";
Use the C# coalesce operator: ??
// if Value is not null, newValue = Value else if Value is null newValue is YournullValue
var newValue = Value ?? YourNullReplacement;
To extend #Dave's answer...if planRec.approved_by is already a string
this.approved_by = planRec.approved_by ?? "";
The accepted answer was correct in time, when it was given.
For people still finding this question:
Today you can use the ??= Operator.
e.g:
private string _test = null;
private void InitIfNull(){
_test ??= "Init";
}
To assign a non-empty variable without repeating the actual variable name (and without assigning anything if variable is null!), you can use a little helper method with a Action parameter:
public static void CallIfNonEmpty(string value, Action<string> action)
{
if (!string.IsNullOrEmpty(value))
action(value);
}
And then just use it:
CallIfNonEmpty(this.approved_by, (s) => planRec.approved_by = s);
You can also do it in your query, for instance in sql server, google ISNULL and CASE built-in functions.
I use extention method SelfChk
static class MyExt {
//Self Check
public static void SC(this string you,ref string me)
{
me = me ?? you;
}
}
Then use like
string a = null;
"A".SC(ref a);

Categories

Resources