Explicit vs implicit call of toString

There’s little difference. Use the one that’s shorter and works more often. If you actually want to get the string value of an object for other reasons, and want it to be null friendly, do this: String s = String.valueOf(obj); Edit: The question was extended, so I’ll extend my answer. In both cases, they compile … Read more

How to create an extension method for ToString?

Extension methods are only checked if there are no applicable candidate methods that match. In the case of a call to ToString() there will always be an applicable candidate method, namely, the ToString() on object. The purpose of extension methods is to extend the set of methods available on a type, not to override existing … Read more

Override valueof() and toString() in Java enum

You can try out this code. Since you cannot override valueOf method you have to define a custom method (getEnum in the sample code below) which returns the value that you need and change your client to use this method instead. public enum RandomEnum { StartHere(“Start Here”), StopHere(“Stop Here”); private String value; RandomEnum(String value) { … Read more

Why is a round-trip conversion via a string not safe for a double?

I found the bug. .NET does the following in clr\src\vm\comnumber.cpp: DoubleToNumber(value, DOUBLE_PRECISION, &number); if (number.scale == (int) SCALE_NAN) { gc.refRetVal = gc.numfmt->sNaN; goto lExit; } if (number.scale == SCALE_INF) { gc.refRetVal = (number.sign? gc.numfmt->sNegativeInfinity: gc.numfmt->sPositiveInfinity); goto lExit; } NumberToDouble(&number, &dTest); if (dTest == value) { gc.refRetVal = NumberToString(&number, ‘G’, DOUBLE_PRECISION, gc.numfmt); goto lExit; } DoubleToNumber(value, … Read more

Negative numbers to binary string in JavaScript

Short answer: The toString() function takes the decimal, converts it to binary and adds a “-” sign. A zero fill right shift converts it’s operands to signed 32-bit integers in two complements format. A more detailed answer: Question 1: //If you try (-3).toString(2); //show “-11” It’s in the function .toString(). When you output a number … Read more

How do I automatically display all properties of a class and their values in a string? [duplicate]

I think you can use a little reflection here. Take a look at Type.GetProperties(). public override string ToString() { return GetType().GetProperties() .Select(info => (info.Name, Value: info.GetValue(this, null) ?? “(null)”)) .Aggregate( new StringBuilder(), (sb, pair) => sb.AppendLine($”{pair.Name}: {pair.Value}”), sb => sb.ToString()); }

when to use toString() method

In most languages, toString or the equivalent method just guarantees that an object can be represented textually. This is especially useful for logging, debugging, or any other circumstance where you need to be able to render any and every object you encounter as a string. Objects often implement custom toString behavior so that the method … Read more

Dumping a java object’s properties

You could try XStream. XStream xstream = new XStream(new Sun14ReflectionProvider( new FieldDictionary(new ImmutableFieldKeySorter())), new DomDriver(“utf-8”)); System.out.println(xstream.toXML(new Outer())); prints out: <foo.ToString_-Outer> <intValue>5</intValue> <innerValue> <stringValue>foo</stringValue> </innerValue> </foo.ToString_-Outer> You could also output in JSON And be careful of circular references 😉

How to make JSON.Net serializer to call ToString() when serializing a particular type?

You can do this easily with a custom JsonConverter: public class ToStringJsonConverter : JsonConverter { public override bool CanConvert(Type objectType) { return true; } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteValue(value.ToString()); } public override bool CanRead { get { return false; } } public override object ReadJson(JsonReader reader, Type objectType, object … Read more

tech