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

  • Home
  • SEARCH
  • 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 8341469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:29:23+00:00 2026-06-09T05:29:23+00:00

for instance, if I have this code: if (foo != default(foo)) { int foo2

  • 0

for instance, if I have this code:

if (foo != default(foo))
{ 
  int foo2 = foo; 
}

is there a way to shorten this to only the assignment? in pseudocode, something like:
foo2 = if not default foo

  • 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-09T05:29:24+00:00Added an answer on June 9, 2026 at 5:29 am

    The problem with trying to shorten this is that foo2 is only valid within the scope inside your if statement. By moving this into one line, you’d always have to have foo2 defined in the outer scope, and it would always need some value.

    If that is acceptable, you could use C#’s conditional operator:

    int foo2 = foo != default(foo) ? foo : default(int);
    

    Note that you need something for when foo == default(foo), as well, which becomes the final portion. With an int value, I would probably use : 0; at the end, but since you’re checking against default(foo), I’m assuming your “real use case” is probably not an Int32 value…


    Edit:

    The (int) was an afterthought, in what I was actually trying I already had foo2 assigned so this is exactly what I was looking for.

    Given this comment, you could do:

    foo2 = foo != default(foo) ? foo : foo2;
    

    This would effectively reassign foo2 if foo doesn’t have it’s default value, and leave it alone (assign it to itself) if it does.

    That being said, I personally prefer something similar to your original:

    // Assumes foo2 is already defined, based on your comment
    if (foo != default(foo))
        foo2 = foo;
    

    This is, in my opinion, far more clear in terms of your intent, and does avoid the extra assignment to yourself that you’re getting with a conditional operator.

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

Sidebar

Related Questions

If I have this code: var foo1 = new Foo(); var foo2 = new
Suppose I have this code... foo.h @interface Foo : NSObject { NSString *aString; //
I have code that all over the place(probably 20-30 instances) does this: <widget>.setVisible((condition ==
Let's say for an instance I have this string: var a=23434,bc=3434,erd=5656,ddfeto='dsf3df34dff3',eof='sdfwerwer34',wer=4554; How should I
I have this object which is an instance of a superclass. I want to
I have this string for an instance: 2 + 2 - 2 and when
I have this data in a table, for instance, id name parent parent_id 1
I have this string : <meis xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance uri=localhost/naro-nei onded=flpSW531213 identi=lemenia id=75 lastStop=bendi xsi:noNamespaceSchemaLocation=http://localhost/xsd/postat.xsd xsd/postat.xsd>
For instance I have a string like this : abc123[*]xyz[#]098[~]f9e [*] , [#] and
I have something like this: <?xml version=1.0 encoding=utf-8?> <Data> <ConfigDatas> <ArrayOfConfigData xmlns:i=http://www.w3.org/2001/XMLSchema-instance xmlns=http://schemas.datacontract.org/2004/07/BTTest.Models> <ConfigData>

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.