Reputation: 2985
Is there a shorter way to write this in c#:
if(myobject!=null){
}
In JavaScript we can do this:
if(myobject){
}
Disclaimer: I know this will match 'true' as well in JavaScript. This would only be used on variables that should be a specific type of object.
I found some similar questions, but they're asking slightly different things:
C# Shortest Way to Check for Null and Assign Another Value if Not
Best and fastest way to check if an object is null
How to determine if variable is 'undefined' or 'null'?
Upvotes: 5
Views: 2994
Reputation: 186668
You can obtain the same syntax in C# via operator
:
public class MyClass {
...
// True if instance is not null, false otherwise
public static implicit operator Boolean(MyClass value) {
return !Object.ReferenceEquals(null, value);
}
}
....
MyClass myobject = new MyClass();
...
if (myobject) { // <- Same as in JavaScript
...
}
Upvotes: 12
Reputation: 326
If you want to Throw an ArgumentNullException
e.g. to check method parameters, there is a handy one-liner to do this:
_ = x ?? throw new ArgumentNullException(nameof(x));
Here, we try to assign the parameter to the discard _
operator. The ??
-operator performs a nullcheck. If the variable is null, the exception is thrown.
In Visual Studio you can add a custom snippet to bind this line to the shortcut arg0
. You only need to type arg0
, double press the TAB key, and to type the parameter name. Implementing a null check then only takes 2 seconds.
Here is the snippet. To import it into Visual Studio, please use this guide: https://learn.microsoft.com/de-de/visualstudio/ide/walkthrough-creating-a-code-snippet?view=vs-2019
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Argument null check for parameters</Title>
<Shortcut>arg0</Shortcut>
</Header>
<Snippet>
<Code Language="CSharp">
<![CDATA[_= $param$ ?? throw new ArgumentNullException(nameof($param$));]]>
</Code>
<Declarations>
<Literal>
<ID>param</ID>
<ToolTip>Name of the parameter.</ToolTip>
<Default>x</Default>
</Literal>
</Declarations>
</Snippet>
</CodeSnippet>
Upvotes: 1
Reputation: 2458
used ?? Operator https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operator
var myobject = notNullValue ?? nullValue;
Upvotes: 2
Reputation: 39085
C# language philosophy is quite different than that of JavaScript. C# usually forces you to be more explicit about somethings in order to prevent some common programming errors (and I'm sure this also helps simplify the compiler design & test).
If C# had allowed such an implicit conversion to boolean, you are much more likely to run into programming errors like this:
if(myobject = otherObject)
{
...
}
where you've made an assignment instead of an equality check. Normally C# prevents such mistakes (so although Dmitry's answer is clever, I'd advise against it).
Upvotes: 8
Reputation: 246
You can use object class static method ReferenceEquals method to find out if the refrence is null or not
MyClass1 obj = new MyClass1();
if (object.ReferenceEquals(obj,null))
{
Console.Write("obj ref is null");
}
Upvotes: -6