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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:51:18+00:00 2026-05-14T15:51:18+00:00

Which coding style do you prefer: object o = new object(); //string s1 =

  • 0

Which coding style do you prefer:

object o = new object();
//string s1 =  o ?? "Tom"; // Cannot implicitly convert type 'object' to 'string' CS0266
string s3 = Convert.ToString(o ?? "Tom");
string s2 = (o != null) ? o.ToString() : "Tom";

s2 or s3?

Is it possible to make it shorter? s1 does not obviously work.

  • 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-14T15:51:19+00:00Added an answer on May 14, 2026 at 3:51 pm

    In this case, I think my preference would be:

    string s1 = (string)o ?? "Tom";
    

    Or:

    string s1 = (o as string) ?? "Tom";
    

    Depending on whether or not o is really expected to be a string or not. Either way, I prefer these because they better express what’s being done, and don’t pass this through an unnecessary conversion if o is already a string.


    As a general rule, I prefer whichever is clearer and/or actually works. When working with strings, I often need to write something like this instead:

    string result = !string.IsNullOrEmpty(value) ? value : "Default";
    

    …which can’t really be done at all with the null-coalescing operator. On the other hand, if I’m trying to coalesce a large number of values, it’s about 500 times better:

    var result = firstTry ?? secondTry ?? thirdTry ?? fourthTry ?? fifthTry;
    

    Try writing that with the ternary operator instead.

    If the difference is not this dramatic, if it’s just going to be a couple of characters on one line of code, it really doesn’t matter, just use whatever you’re comfortable with.

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

Sidebar

Related Questions

here is my coding which gives me the error 'warning: unknown conversion type character
I have a coding style question which probably should be asked of a senior
I'm new to funcctional programming and have some questions regarding coding style and debugging.
In a coding style question about infinite loops , some people mentioned they prefer
Which one is better Java coding style? boolean status = true; if (!status) {
Two questions really... Firstly, which is the better coding style when writing jQuery functions
I'm looking for a practical C++ coding style for medium to large projects which
Is there any pdf which tells about coding guidelines in objective C. For Example...
Which is the strictest mode of XHTML to make my coding habits semantically and
I'm coding a windows program which is a WYSIWYG form generator. In a very

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.