unsigned integers and CLS compliance

  • Thread starter Thread starter Erik Frey
  • Start date Start date
E

Erik Frey

Hi,

I'm writing a class that parses a file format that contains unsigned 32
bit integers. I've discovered that data types like UInt32 are not
CLS-compliant, and so I've been trying to figure out the best way to handle
this.

I could store them in Int64's (seems excessive, and presents problems
when trying to cast back to Int32 for function calls), or store them in
standard Int32's and have logical everywhere to check for Int32's that
evaluate "negative" and handle overflow...

There must be a better way to deal with this. And finally, why aren't
unsigned integers CLS-compliant!? I don't see what's so complicated or
nonstandard about that number format.

Thanks,

Erik
 
Hi Erik,

You can use unsigned integers in the implementation of your type. Just
don't expose them publicly. If you want to double check yourself, add this
to the top of your file.

[assembly:CLSCompliant(true)]

It will generate a compiler error when you slip up.

CLS Compliance is the intersection of all managed languages, defining what
each language can expose if it wants to interoperate with other .NET
languages. With this in mind, consider that all managed languages do not
support unsigned types. So, exposing uint in your public interface means
that code written in other languages can't use your type.

Joe
 
Joe,

This is exactly what I wanted to hear - thanks!

So my plan will be to use unsigned integers internally, but expose them
as Int64's.

Erik

Joe Mayo said:
Hi Erik,

You can use unsigned integers in the implementation of your type. Just
don't expose them publicly. If you want to double check yourself, add this
to the top of your file.

[assembly:CLSCompliant(true)]

It will generate a compiler error when you slip up.

CLS Compliance is the intersection of all managed languages, defining what
each language can expose if it wants to interoperate with other .NET
languages. With this in mind, consider that all managed languages do not
support unsigned types. So, exposing uint in your public interface means
that code written in other languages can't use your type.

Joe
--
http://www.csharp-station.com

Erik Frey said:
Hi,

I'm writing a class that parses a file format that contains unsigned 32
bit integers. I've discovered that data types like UInt32 are not
CLS-compliant, and so I've been trying to figure out the best way to handle
this.

I could store them in Int64's (seems excessive, and presents problems
when trying to cast back to Int32 for function calls), or store them in
standard Int32's and have logical everywhere to check for Int32's that
evaluate "negative" and handle overflow...

There must be a better way to deal with this. And finally, why aren't
unsigned integers CLS-compliant!? I don't see what's so complicated or
nonstandard about that number format.

Thanks,

Erik
 
Back
Top