Zytan wrote:
http://www.devarticles.com/c/a/C-Sha...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.
--
Tom Porterfield