CLS complaint = Operators should not be overloaded

Z

Zytan

http://www.devarticles.com/c/a/C-Sharp/Making-Your-Code-CLS-Compliant/

Does anyone follow this? I know it's good to not have to deal with
unsigned types in the interface, so that's great. But CLS compliance
is quite strict:

"Only properties and methods may be overloaded, Operators should not
be overloaded."

Does that mean I can't make my own + operator for my own object? I
find this hard to believe. But, in general, it seems that CLS
compliant code is a very good idea. I wonder how widespread its usage
is? Do you guys use it?

Zytan
 
T

Tom Porterfield

Zytan said:
http://www.devarticles.com/c/a/C-Sharp/Making-Your-Code-CLS-Compliant/

Does anyone follow this? I know it's good to not have to deal with
unsigned types in the interface, so that's great. But CLS compliance
is quite strict:

"Only properties and methods may be overloaded, Operators should not
be overloaded."

Does that mean I can't make my own + operator for my own object? I
find this hard to believe. But, in general, it seems that CLS
compliant code is a very good idea. I wonder how widespread its usage
is? Do you guys use it?

The short answer is that not every .NET language supports operator
overloading so it is not part of the CLS. That doesn't mean you can't
overload operators in your code, but if you do you need to mark that section
as CLSComplaint(false) and give an alternate way of doing the same thing for
..NET languages that do not support operator overloading. For example, if
you overload + then also give your class an Add method that does the same
thing.

As far as CLS compliant code being a very good idea, that is only true if
you plan on making your objects available to any .NET language. If you know
your consumers are all going to be written in languages that support
operator overloading, and it makes sense in your class to do that, then you
shouldn't necessarily feel restricted by CLS compliance.
 
J

Joerg Jooss

Thus wrote Zytan,
http://www.devarticles.com/c/a/C-Sharp/Making-Your-Code-CLS-Compliant/

Does anyone follow this? I know it's good to not have to deal with
unsigned types in the interface, so that's great. But CLS compliance
is quite strict:

"Only properties and methods may be overloaded, Operators should not
be overloaded."

That is not correct. If you overload an operator, you *should* provide an
alternative method that fulfills the same purpose. This keeps your type fully
usable for .NET languages that don't support operator overloading (see System.DateTime
for example).

Cheers,
 
Z

Zytan

The short answer is that not every .NET language supports operator
overloading so it is not part of the CLS. That doesn't mean you can't
overload operators in your code, but if you do you need to mark that section
as CLSComplaint(false) and give an alternate way of doing the same thing for
.NET languages that do not support operator overloading. For example, if
you overload + then also give your class an Add method that does the same
thing.

Oh, ok.
As far as CLS compliant code being a very good idea, that is only true if
you plan on making your objects available to any .NET language. If you know
your consumers are all going to be written in languages that support
operator overloading, and it makes sense in your class to do that, then you
shouldn't necessarily feel restricted by CLS compliance.

Wonderful explanation, Tom. Thanks!

Zytan
 
Z

Zytan

"Only properties and methods may be overloaded, Operators should not
That is not correct. If you overload an operator, you *should* provide an
alternative method that fulfills the same purpose. This keeps your type fully
usable for .NET languages that don't support operator overloading (see System.DateTime
for example).

Great, and this totally makes sense. So, I can do it, and people who
use the more powerful languages can take advantage of it, but for
those without such features, they can still use my unit!

Thanks, Joerg!

Zytan
 

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