Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7613211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:06:08+00:00 2026-05-31T02:06:08+00:00

This pattern comes up very frequently in my code: x= x== 0? 1: x;

  • 0

This pattern comes up very frequently in my code:

x= x== 0? 1: x;
//or
x= x==null? 1: x;

However it happens that sometimes x is a long expression and I’d have to use intermediate variables. That’s just useless boilerplate code. I can cook up a method and call it instead:

Util.IfNullOrZero(x, 1);

But that’s just ugly. What is the best way of expressing the pattern? In ruby there is such syntax for when x is nil which gets rid of redundant x’s:

x||= 1

I could extend object in a manner

public static class wtf
{
    public static T Default<T>(this object o, T d)
    {
        return o == null ? d : new object[] { o }.Cast<T>().First();
    }
}

And then do

object param= null;
int x= param.Default(1);

But that’s a bit expensive.

In short how to best make C# do x||= 1 like in ruby?

Update

This is what I cooked up. I’m currently looking for a faster way of using the Template parameter to convert object to T.

public static class MyExtensions
{
    public static T d<T>(this object o, T d)
    {
        return o == null || o.Equals(default(T)) ? d : new object[] { o }.Cast<T>().First();
    }
}

In fact the code does three things at once: Casts to default type, checks for default value and also checks for null.

Update 2

return o == null || o.Equals(default(T)) ? d : (T)o; // much simpler and faster

I still think it is a commonality which needs to be included in core language.

Update 3
This is what I finally wrote, taking into account DataTable DBNull types.

public static T d<T>(this object o, T d)
{
    return o == null || (o is System.DBNull) || o.Equals(default(T)) ? d : (T)Convert.ChangeType(o, typeof(T));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:06:09+00:00Added an answer on May 31, 2026 at 2:06 am

    For checking for null and providing a default value, you can use the ?? operator:

    return x ?? new Foo();
    

    That means, if x is null, return new Foo(), else return x. You can use it for reference types and nullable types. For nun-nullable types like int, you still need to explicitly check for 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this pattern written ^.*\.(?!jpg$|png$).+$ However there is a problem - this pattern
I use this code pattern all over and only in this particular property an
Folks, I have a feeling there's a classic design pattern that covers this case,
This pattern pops up a lot. It looks like a very verbose way to
My URLconf contains this pattern: url(r'^accounts/logout/$','django.contrib.auth.views.logout', name=logout), And I've trying to reverse that in
I have this pattern: ~^([a-z0-9]+[a-z0-9-]+[a-z0-9]+\.([a-z]+)(\.[a-z]+)?)$~i It will match the following: xxx.xxx or xxx.xxx.xxx (number
I was wondering if this is an 'unwholesome' use of a factory pattern, or
I have got codes to match with a very simple string pattern: XXnnnnnnnnn (2
I am trying to use ifeq in my rule pattern and I have problems
So this pattern: def foo(&block) block.call end foo lambda { puts 'hi' } Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.