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 8840129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:23:42+00:00 2026-06-14T10:23:42+00:00

Does C# have a similar operation to JavaScript’s || setter? For example, in JavaScript,

  • 0

Does C# have a similar operation to JavaScript’s || setter?

For example, in JavaScript, if I want to check if a value is null and set a default, I can do something like this:

function foo(val){
    this.name = val || "foo";
}

But, when I want this functionality in C# I have to go this route:

public Foo(string val)
{
    this.name = (string.IsNullOrEmpty(val) ? "foo" : val);
}

I know this is petty and that beggars can’t be choosers but, if at all possible, I’d like to avoid getting any answers that involve crazy hacks or extension methods. I’m fine with using the ternary operator, but I was just curious if there was a language feature I’d missed.

Update:

Great answers all around, but J0HN and Anton hit it on the head. C# doesn’t do “falsy” values like JavaScript would in the example above, so the ?? operator doesn’t account for empty strings.

Thanks for your time and the great responses!

  • 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-06-14T10:23:43+00:00Added an answer on June 14, 2026 at 10:23 am

    There is a ?? operator that essentially is the same as COALESCE operator in SQL:

    int? a = null; //int? - nullable int
    int q = a??1; // q is set to one;
    

    However, ?? does not check the string for emptiness, so it does not share the same meaning as javascript’s ||, which treats empty strings as false as well.

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

Sidebar

Related Questions

Does .NET have anything similar to Perl arrays, which are indexed numerically but automatically
I am just trying to find out that does puppet have something similar to
Does anyone have a translate function for x/y positions after rotation in javascript? for
Both offer policies which have to be enforced and are similar in operation. There
I have a JavaScript object those properties are static, mostly. They can be determined
How can I do an atomic operation on a shared memory? I have something
Does MATLAB have a function/operator that indicates the type of a variable (similar to
How to check for a new line in a string? Does python3.x have anything
Does ruby on rails have an equivalent to ++value. In c there's a ++x
Does ** have any special meaning in C? Like this: static intparse_one (int argc,

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.