Information Technology Reference
In-Depth Information
Yo u c a n n o t b u i l d a C L S - c o m p l i a n t a s s e m b l y i f y o u h a v e t y p e s i n y o u r p u b -
lic or protected interface that are not CLS compliant. If, as a component
designer, you do not have an assembly marked as CLS compliant, you
make it harder for users of your component to create CLS-compliant
assemblies. They must hide your types and mirror the functionality in a
CLS-compliant wrapper. Yes, this can be done. But, no, it's not a good way
to treat the programmers who want to use your components. It's better to
strive for CLS-compliant assemblies in all your work: This is the easiest way
for clients to incorporate your work in their CLS-compliant assemblies.
The second rule is up to you: You need to make sure that you provide a
language-agnostic way to perform all public and protected operations. You
also need to make sure that you do not sneak a noncompliant object
through your interface using polymorphism.
Operator overloading is a feature that some love and others hate. As such,
not every language supports or allows operator overloading. The CLS stan-
dard does not take a pro or con stance on the concept of operator over-
loading. Instead, it defines a function name for each operator: op_equals
is the function name created when you write an operator = function.
op_add is the name for an overloaded addition operator. When you write
an overloaded operator, the operator syntax can be used in languages that
support overloaded operators. Developers using a language that does not
support operator overloading must use the op_ function name. If you
expect these programmers to use your CLS-compliant assembly, you
should provide a more convenient syntax. That leads to this simple rec-
ommendation: Anytime you overload an operator, create a semantically
equivalent function:
// Overloaded Addition operator, preferred C# syntax:
public static Foo operator +( Foo left, Foo right)
{
// Use the same implementation as the Add method:
return Foo .Add(left, right);
}
// Static function, desirable for some languages:
public static Foo Add( Foo left, Foo right)
{
return new Foo (left.Bar + right.Bar);
}
 
Search WWH ::




Custom Search