NOT NULL method? Shorthand to use in a conditional

I’m looking for shorthand - the most efficient way, really - to specify a check for something NOT BEING NULL.

PHP has the !empty() method, which will check for null, an empty string (“”), etc.

example:

if(!empty(x))
{
do;
}

.NET has the !IsPostBack to check for a postback, but is there a way to go:

if(!null(x))
{
do;
}

instead of

if(x != null)
{
do;
}

I want to know because I’m a fan of the shorthand if statements…

string xyz += (!null(x))?"this is not null":"this is null";

and it’d keep things efficient.

There’s a string function but I wouldn’t call it shorthand.

if (!String.IsNullOrEmpty(x))

Otherwise

if (x != null)

like NAWA-mark said.

but according to char count x!=null is shorter that !null(x) if that was the short hand. lol

There is short hand for if null or something like that. eg:

Client c = savedClient ?? new Client();

That sets c to savedClient if its not null else create a new instance. If that makes sense

Yeah, I knew there was one I was missing. :slight_smile:

if(!null(x))

is actually longer (by 1 character) than

if(x != null)

…if you strip out the spaces. :shifty:

Haha. This is true.

But it’s just the logic that goes with the shorthand if statements.

x = (!null(y))?“yes”:“no”;

“x equals not-null y then yes, null y then no”

is just smoother - to me - than

x = (y != null)?“yes”:“no”;

“x equals if y is not null then yes, if y is null then no”

Or a simple extension method:


 public static bool IsNull(this object input)
        {
            return (input == null);
        }

        public static bool IsNotNull(this object input)
        {
            return (input != null);
        }

I love this. Thanks for the insight.

Here is some more info on extension methods: http://msdn.microsoft.com/en-us/library/bb383977.aspx

Use Responsibly :wink: