Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does System.Guid not implement System.IConvertible?

Tags:

c#

guid

clr

I recently tried to return an object of type Guid from a method accepting <T>, however the compiler gave me the following error:

The type 'System.Guid' cannot be used as type parameter 'T' in the generic type or method 'MyGenericMethod'. There is no boxing conversion from 'System.Guid' to 'System.IConvertible'.

After investigation I realised that the compiler message was caused due to the Guid type not implementing the System.IConvertible interface.

MSDN states the following:

This interface provides methods to convert the value of an instance of an implementing type to a common language runtime type that has an equivalent value.

The provided list of types does not include Guid; Can anyone explain/provide a use case as to why this is the case?

like image 270
Jamie Keeling Avatar asked Oct 25 '12 10:10

Jamie Keeling


People also ask

Does string implement IConvertible?

String does not implement the interface System. IConvertible.

What is IConvertible interface in C#?

The IConvertible interface allows conversion of an object to basic data types and allows the conversion methods in the Convert class to use that object. When implementing the IConvertible interface, create your own type-specific methods for each of the supplied conversion methods.

What is System IConvertible?

This interface provides methods to convert the value of an instance of an implementing type to a common language runtime type that has an equivalent value. The common language runtime types are Boolean, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal, DateTime, Char, and String.


1 Answers

IConvertible requires the type be able to convert it's data to most of the primitives. How would you represent a Guid as a float for example?

Because Guid cannot implement most of the interface methods it's expected to not declare itself otherwise.

Now on the real question: What are you trying to accomplish?

like image 106
Adrian Zanescu Avatar answered Oct 24 '22 00:10

Adrian Zanescu