Is KeyValuePair<string,string>.ToString intended as serialization building block?

S

Steve Richter

KeyValuePair<string,string> has a ToString method that returns the
KeyValue pair seperated by a comma and enclosed in [ ]:
[ name, Jane ]

Is this method used as a building block for serialization?

The reason I ask is because I dont see a "FromString" or "Parse"
static method that takes a string as an argument and returns a
KeyValuePair<string,string> object.

also, the ToString resulting value does not look like it could be
parsed under all circumstances. If the value in the KeyValuePair
contains a comma the ToString method returns the value as is, without
enclosing it in quotes:

KeyValuePair<string,string> demo
= new KeyValuePair<string,string>( "First, Names", "Billy,
Jane" ) ;
MessageBox.Show(demo.ToString());

resulting string: [First, Names, Billy, Jane]

-Steve
 
A

Andy

KeyValuePair<string,string> has a ToString method that returns the
KeyValue pair seperated by a comma and enclosed in [ ]:
[ name, Jane ]

Is this method used as a building block for serialization?

The reason I ask is because I dont see a "FromString" or "Parse"
static method that takes a string as an argument and returns a
KeyValuePair<string,string> object.

also, the ToString resulting value does not look like it could be
parsed under all circumstances. If the value in the KeyValuePair
contains a comma the ToString method returns the value as is, without
enclosing it in quotes:

KeyValuePair<string,string> demo
= new KeyValuePair<string,string>( "First, Names", "Billy,
Jane" ) ;
MessageBox.Show(demo.ToString());

resulting string: [First, Names, Billy, Jane]

-Steve

No, its not. Its simply there to get a string version of the
instance. All .Net objects have a ToString method; most simply use
the default implementation to return the type name, but key value
pair, overrides to call ToString on both the key object and value
object (which in your case are both strings).

Serialization would be done by decorating a class with the
SerializationAttribute.
 
N

Nicholas Paldino [.NET/C# MVP]

Steve,

The ToString method is not meant to be used for Serialization. The
ToString method is located on the Object class, and is not intended to be
used for serialization. Rather, its intention is to return a human-readable
string that is culture-sensitive (from the documentation).

This is why you do not see a FromString method.

Generally, I would use the TypeConverter class (rather, a class derived
from that) to perform the conversion. However, it would be up to the
KeyValuePair<TKey, TValue> instance to indicate what the TypeConverter
should be, which it does not.

Is there something specific you are trying to do? The structure is
serializable (assuming that TKey and TValue are serializable) but that would
allow you to serialize the instance to a binary stream, or a xml message.
You can also try XML serialization as well.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top