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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:08:26+00:00 2026-05-26T18:08:26+00:00

i would like to override the ToString() method for an int (that is a

  • 0

i would like to override the ToString() method for an int (that is a part of a class) so that if the value of the int is 0, the ToString() should return an empty string "".Can this be done?

UPDATE
It would be easy to just create a

public string AmountToString {
    get { if (Amount != 0) return Amount.ToString(); else return ""; }
}

i was just curious to see if it could be implemented (the ToString() ) on an primite type

  • 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-26T18:08:27+00:00Added an answer on May 26, 2026 at 6:08 pm

    Three main approaches:

    Employ a (custom) Format provider

    So you can use i.ToString(formatprovider);

    Edit I think I found a custom format string one that works with the default .NET number format provider:

    i.ToString("0;-0;"); // works
    

    Another sample lifted from the page on The “;” Section Separator:

    i.ToString("##;(##);**Zero**"); // would return "**Zero** instead
    

    Tip:

    You can download the Format Utility, an application that enables you to apply format strings to either numeric or date and time values and displays the result string.

    Extension method

    public static string ToStringOrEmpty(this int i)
    {
        return (0==i)? string.Empty : i.ToString();
    }
    

    Accessor Helper:

    class X
    { 
          int member;
    
          public string getMember() { return (0==member)? string.Empty : member.ToString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class MyClass, and I would like to override the method ToString()
I'm building a console based inventory program and would like to override the toString()
I would like to temporarily override the kill-new function. I have a way I
Would like a for loop in jquery so that: For every hover_link: show hidden
Would like to know what a programmer should know to become a good at
Would like to make anapplication in Java that will not automatically parse parameters used
In our domain, we override the ToString method on all our domain types. However,
My problem here is that I would like to pass an object to a
Let's say I have a class like this (and also further assume that all
I would like to know what are the functions that the Error object of

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.