Lack of Unsigned CLS-Compliant Types in .NET

T

Tim Rogers

I'm wrapping a native C++ API with MC++. One of the things that I have
found frustrating is the fact that most (if not all) unsigned types in .NET
are not CLS-Compliant. So, to me, that means you can't use them, say, as
arguments in a method because a particular .NET language is not required to
support those types. The C++ API has a lot of paramters that are U8, U16,
and U32 types. Since the equivalent types in .NET are not CLS-compliant, I
find my self using a larger signed type to hold the possible non-negative
values of the corresponding unsigned type. For instance, for a U32 in the
C++ API, I have to use an Int64 in .NET. While, this works, it's just sort
of messy. For example, it forces you to do runtime checks on numbers that
are larger than can fit in a U32 in case someone passes in a really large
number in a method call (e.g. someone calling a method that takes an Int64
could pass in 4,294,967,306 - this is too large for a U32).

I was just curious if there is a better way to handle this? Not making
unsigned types CLS-compliant seems somewhat short-sighted to me, but maybe
I'm wrong. Any thoughts?

Thanks,

Tim Rogers
 
M

Michael Lang

Tim Rogers said:
Not making unsigned types CLS-compliant seems somewhat short-sighted
to me, but maybe I'm wrong. Any thoughts?

Thanks,

Tim Rogers

CLS compliant just means that ALL languages and platforms must support it.
If something is not CLS compliant, it was probably forseen as impossible to
implement on another target platform or language. Or as just to difficult
of a concept to implement in some language (VB7).

C# does support unsigned types. If you don't mind only C# as a viable user
of your component, just leave the types as unsigned.

You could also re-write your C++ component to use a signed type.
 
T

Thomas Scheidegger [MVP]

The C++ API has a lot of paramters that are U8, U16,
and U32 types.

yes, but many/most of the API calls don't depend on the sign.
So just use the signed .NET types.
 
R

Richard Grimes [MVP]

As a C++ programmer I also find it odd that other languages don't support
unsigned types, but its just a fact of life and you have to accept it :-(

However, to make your C# and MC++ developers happy you can use overloading
to provide a non-CLS compliant version of your methods that use unsigned
parameters - just make sure that they are marked with [ClsCompliant(false)]
so that signed-challenged languages don't call them (and call the version
with signed parameters instead).

Richard
 

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