Override ToString () for an int element in a class

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

UPDATE It would be easy to create

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

I was just curious to know if it can be implemented (ToString ()) in a primitive type

+4
source share
11 answers

Three main approaches:

Use the (custom) format provider

So you can use i.ToString (formatprovider);

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

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

Another sample removed from the page ; Partition Separator :

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

Tip:

You can d download the Format utility , an application that allows you to apply format strings to digital or dates and times and displays the result string.

Extension method

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

Helper Assistant:

 class X { int member; public string getMember() { return (0==member)? string.Empty : member.ToString(); } 
+10
source

Unfortunately not. You need to have access to the class code to override any methods or properties, including the ToString method. Since int is a primitive type, you cannot change the class code (neither easy nor reliable).

It is best to create an extension method:

 public static class IntExtensions { public static ToStringOrEmpty(this int value) { return value == 0 ? "" : value.ToString(); } } 
+1
source

If this is not a method of this class, you cannot change the implementation of int ToString() .

Some options:

  • Create a method for the class.
  • Create an extension method for int , however you will have to control its call (unlike ToString ).
  • Another option is to create a wrapper around int, with implicit conversion operators, etc., that implements ToString as you want, and use it in your class.
  • Create or locate a custom format provider that you can pass to ToString .
+1
source

You can define an extension method in the same namespace and override the behavior for the int class. Example Example:

 void Main() { Console.WriteLine(5.Convert()); // Prints "5" Console.WriteLine("Empty String" + 0.Convert()); } // Define other methods and classes here public static class ExtMethods{ public static string Convert(this int value) { return value == 0 ? string.Empty : value.ToString(); } } 
+1
source

You must override the ToString method in (your) class itself, and when it is called, just return the .Empty line when this field is 0.

 return a+i==0?string.Empty:i+... 
0
source

If you write a type that inherits from int, but , unfortunately, int is a private class, so you cannot.

0
source

I want better write a som extension method ...

 public static string ToMyString(this int input) { return input == 0 ? string.Empty : input.ToString(); } 
0
source

I had never heard of how to do this before, and I did not find it when searching. You can use your own class that contains int and has its own overridden toString (). it is important? or should you have it as an int?

you can also use

 x == 0 ? "" : x.toString(); 

so you get "" if 0 or tostring

0
source

What you want to do is override the class whose ToString () you want to implement. The int type is a private class, since most base types.

What you can do is a class that encapsulates an int, and then implements casting operations and overrides the ToString () method.

It sounds like a lot of work for something so small, but it's the way to go!

Here is your class (sorry for the lousy names):

 struct b { int myInt; public override string ToString() { return myInt != 0 ? myInt.ToString() : ""; } public static implicit operator int(bd) { return d.myInt; } // User-defined conversion from double to Digit public static implicit operator b(int d) { return new b { myInt = d }; } } 
0
source

You cannot override the IntString () method for int since you will not have access to the code, but you can create an extension method in the int class with a different method name, for example ToString2 (), in which you can return an empty string when the int value is equal to zero, you can return the normal value of the int variable.

For help on how to create extension methods for existing classes, you can refer to the link below:

http://msdn.microsoft.com/en-us/library/bb311042.aspx

0
source

Create Extension for Int

 public static class Ext { public static string ToMyString(this int myInt) { return myInt == 0 ? "" : myInt.ToString(); } } 
0
source

Source: https://habr.com/ru/post/1380554/


All Articles