October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Write CLS-Compliant Public APIs in C#

Declare CLS compliance for a C# assembly, review its full public API, and isolate exceptions with documented compliant alternatives.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To make a C# library’s public API CLS-compliant, declare that intent with [assembly: CLSCompliant(true)], build and review the resulting warnings, and explicitly mark any unavoidable non-compliant public types or members with [CLSCompliant(false)]. Offer a compliant alternative where practical. CLS rules govern the component’s public interface—not its private implementation—and help make APIs consumable by languages that support the Common Language Specification.

What CLS compliance means for a C# library

The Common Language Specification (CLS) defines rules for features exposed by components so that code written in different CLS-supporting languages can consume them. For a library author, the key boundary is the public API: private implementation details do not have to comply. Microsoft explains this distinction in its language-independence guidance.

As an Amazon Associate I earn from qualifying purchases.

CLS compliance is therefore an interoperability design goal, not a requirement that every implementation detail use a restricted subset of C#. It is especially relevant when you intend other .NET languages to use your library. A C#-only API may still choose language-specific features, but those features can limit which languages can use the affected public surface.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Declare compliance at the assembly level

When the assembly is intended to expose a CLS-compliant API, declare that intent once at assembly level:

using System;

[assembly: CLSCompliant(true)]

namespace ExampleLibrary
{
    public class Converter
    {
        public int ConvertValue(int value) => value;
    }
}

Place the assembly attribute after any using directives and before declarations. The assembly’s compliance status is inherited by its contained types and members unless you explicitly identify an exception. Microsoft documents the attribute’s behavior in the CLSCompliantAttribute API reference.

Build the project and review compiler warnings about declarations that are presumed compliant but violate CLS rules. Treat warnings as useful diagnostics, not as a complete audit: Microsoft notes that some rules are enforced by compilers even without the attribute, and the selected examples in its overview are not the complete specification.

Review the whole public surface

Check more than method parameters. Review public and protected types and members, including names, fields, properties, method signatures, enum definitions, generic declarations, interfaces, events, and exception types. The following are practical warning signs:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Identifiers that differ only by case: Names such as Person and person can collide in case-insensitive languages, so they are not CLS-compliant as a public API.
  • Unsigned types in public signatures: Microsoft gives a public method using UInt32 as an example of a non-compliant declaration. Do not assume every C# primitive type is suitable for a shared language-facing API.
  • Enum backing types: CLS-compliant enum underlying types are Byte, Int16, Int32, or Int64. An enum backed by UInt32 is a non-compliant example.
  • Interface members: The Microsoft overview lists static methods and fields on CLS-compliant interfaces as disallowed.
  • Thrown exceptions: Throw System.Exception or a type derived from it.
  • Generics and events: Rules also cover matters such as nested generic type parameters, generic type naming, and event naming patterns. Validate the exact design against the standard rather than relying on a short checklist.

Microsoft’s overview describes selected rules and points to ECMA-335, Partition I, Clauses 7–11—especially Clause 11—for the full rule set. Consult that normative text when a public API design depends on an edge case.

Isolate exceptions and offer a compliant alternative

If a useful public feature cannot be made CLS-compliant, mark the affected public type or member explicitly and, where feasible, provide a compliant alternative. For example:

using System;

[assembly: CLSCompliant(true)]

public class IdentifierParser
{
    public int Parse(string value) => int.Parse(value);

    [CLSCompliant(false)]
    public uint ParseUnsigned(string value) => uint.Parse(value);
}

Document the compliant route so consumers using other CLS-supporting languages can find it. A member cannot be declared compliant when its containing type is non-compliant; mark the actual exception at the appropriate public type or member boundary.

CLSCompliantAttribute can be applied at several program-element targets, but applying it to a parameter or return value has no effect: Microsoft’s API reference says those applications are ignored. Use the attribute to state assembly intent and identify non-compliant assemblies, types, or members—not to annotate individual arguments or return values.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A practical compliance workflow

  1. Set the interoperability goal. Decide whether consumers in other CLS-supporting .NET languages are a supported audience.
  2. Declare the default. Add [assembly: CLSCompliant(true)] after using directives and before declarations.
  3. Build and review diagnostics. Investigate warnings on public declarations, then manually inspect the full public surface rather than assuming a clean build proves compliance.
  4. Resolve each exception deliberately. Redesign the API for compliance when practical; otherwise isolate the non-compliant type or member with [CLSCompliant(false)] and document a compliant alternative where feasible.
  5. Check the complete rules for edge cases. Use ECMA-335, Partition I, Clauses 7–11, when the Microsoft overview does not settle a design question.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What the attribute can—and cannot—tell consumers

The attribute communicates an element’s stated compliance status, with status flowing from assembly to types and from types to members. It does not itself redesign an incompatible signature or guarantee that every language compiler or toolchain will behave identically. Use compiler diagnostics alongside deliberate API review, and validate version-sensitive edge cases against the target standard and toolchain.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.