Profilo di StevenDevelopmentalBlogElenchi Strumenti Guida

Blog


20 agosto

System.IConvertible - Its not a Ferrari

You might be familiar with the System.Convert class, which has a number of methods for converting from/to different types. For example:

short s = Convert.ToInt16(int.MaxValue);

We live in a world of casting and you might find this code somewhat pointless. After all, it will still raise StackOverflowException and lose precision with floating poing conversions, so what's the point?

The convert class is capable of converting an instance of ANY type into one of the common base types (sting, int, ulong, char, etc). If a type implements the System.IConvertible interface, it can define how conversions of that type will occur.

Lets consider a (not so practical) example. Say we have a product class with the following properties:

public int ProductId { get; set; }
public DateTime DateAvailable { get; set; }
public string Name { get; set; }

Our Product class also has a parameterless constructor. In this example, we also make the Product class implement the IConvertible interface. This forces us to implement 16 methods and 1 property. Those methods all relate directly to conversion. For example, here's the implementation of one of those methods:

public ulong ToUInt64(IFormatProvider provider) {
      return (UInt64)ProductId;
}

What we are saying here is that if someone tries to convert our Product instance to a ulong then it will simply assign the ProductId. Our test code looks like this:

Product p = new Product { Name="Car", DateAvailable=DateTime.Now, ProductId=7 };
UInt64 i = Convert.ToUInt64(p);
Console.WriteLine(i);

The output is the number "7" because that is how our implementation of IConvertible tells the Convert class to handle conversions to UInt64.

Simple eh?

 

Commenti

Attendere...
Il commento immesso è troppo lungo. Immetti un commento più breve.
Immissione non effettuata. Riprova.
Impossibile aggiungere il commento al momento. Riprova più tardi.
Per aggiungere un commento è necessaria l'autorizzazione di un genitore. Chiedi autorizzazione
I tuoi genitori hanno disattivato i commenti.
Impossibile eliminare il commento al momento. Riprova più tardi.
Hai raggiunto il numero massimo di commenti pubblicabili giornalmente. Riprova tra 24 ore.
Impossibile lasciare commenti. La funzionalità è stata disattivata perché i sistemi hanno rilevato una possibile attività di spamming dal tuo account. Se ritieni che il tuo account è stato disattivato per errore, contatta il supporto tecnico di Windows Live.
Esegui il seguente controllo di protezione per completare la pubblicazione del commento.
I caratteri digitati nel controllo di protezione devono corrispondere ai caratteri dell'immagine o della riproduzione audio.

Per aggiungere un commento, accedi con il tuo Windows Live ID (se utilizzi Hotmail, Messenger o Xbox LIVE possiedi già un Windows Live ID). Accedi


Non hai ancora un Windows Live ID? Registrati

Riferimenti

L'URL di riferimento per questo intervento è:
http://stevennagy.spaces.live.com/blog/cns!B2EFDBF0964586B3!248.trak
Blog che fanno riferimento a questo intervento
  • Nessuno