Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 182 ยท Getting Started

CTS, CLS, Managed Code and Assemblies

Understand the .NET vocabulary behind C# - the Common Type System, the Common Language Specification, managed and unmanaged code, assemblies and metadata, and how .NET Framework became .NET 10.

Read anything about .NET and you meet a pile of acronyms: CTS, CLS, CLR, IL, BCL, "managed code", ".NET Framework", ".NET Core". They are not decoration. They explain why a C# class can be used from F# or Visual Basic, why int and System.Int32 are the same thing, and why an interview question may ask you what "managed" means. This lesson gives each term one clear job, shows the idea in running code, and ends with the history that explains the confusing product names.

If you have not yet read how .NET works, skim it first: it covers the compile-to-IL-to-machine-code pipeline that these terms sit on top of.

The big picture

.NET was designed to let many languages share one runtime. C#, F#, Visual Basic and others all compile to the same intermediate language (IL), and all run on the same runtime. For that to work, the languages must agree on what a type is and what a library may expose. Those two agreements are the CTS and the CLS.

flowchart LR
    CS["C# code"] --> IL["IL + metadata<br/>in an assembly"]
    FS["F# code"] --> IL
    VB["Visual Basic code"] --> IL
    IL --> CLR["CLR: loads, verifies, JIT-compiles,<br/>manages memory"]
    CLR --> OS["Machine code on your CPU"]

Every language produces the same kind of file, so they can call each other.

The Common Type System (CTS)

The Common Type System defines every kind of type the runtime understands and how they behave. It answers questions such as: what is a class, how do value types differ from reference types, what are the rules for inheritance, and how are int or string represented.

The CTS has two big families:

  • Value types: structs, enums and the built-in numeric types. A variable holds the data itself.
  • Reference types: classes, interfaces, delegates, arrays and string. A variable holds a reference to an object on the heap.

Each language gets friendly names for CTS types. The C# keyword int is just an alias for the CTS type System.Int32; Visual Basic calls the same type Integer.

C# C# keywords are CTS types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Because the CTS is shared, a List<int> created in C# can be handed to an F# function that expects System.Collections.Generic.List<int>: both languages see the same type.

The Common Language Specification (CLS)

The CTS is large, and not every language supports all of it. C# has unsigned integers (uint, ulong) and case-sensitive names; Visual Basic is case-insensitive and historically had no unsigned types. If your public library exposed a method that takes a uint, or two members that differ only by letter case, a VB developer could not call it.

The Common Language Specification is the subset of the CTS that every .NET language is expected to understand. A library that stays inside it is CLS-compliant and can be used from any .NET language. The rules include:

  • No public names that differ only by case (Total and total).
  • No unsigned integer types or sbyte in public signatures (they are not in the CLS).
  • No pointers in public signatures.
  • Array element types and indexes must follow simple rules (no non-zero-based arrays).

You ask the compiler to check these rules with an assembly attribute. The code below is read-only, since the project file normally holds the attribute and the effect is a compiler warning:

C#
using System;

[assembly: CLSCompliant(true)]

public class Counter
{
    public int Total() => 0;     // fine
    public uint Big() => 0;      // warning CS3002: return type is not CLS-compliant
    public int total() => 0;     // warning CS3005: differs only by case from 'Total'
}

If you really need a non-compliant member, you can mark just that member [CLSCompliant(false)] and offer a compliant alternative next to it. Most applications ignore CLS compliance, because they are only called by their own C# code. It matters for public libraries (NuGet packages) that want to reach every .NET language.

C# Is an assembly CLS-compliant?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Managed code and unmanaged code

Managed code is code that runs under the control of the runtime (the CLR). The runtime provides services to it: automatic garbage collection, type safety, bounds checking on arrays, exception handling and security checks. C#, F# and VB code is managed by default.

Unmanaged code runs directly on the operating system with no runtime services: C and C++ programs, the Win32 API, native .dll and .so libraries. It must free its own memory, and a bad pointer can corrupt the process.

Managed code Unmanaged code
Compiled to IL, then machine code by the JIT Machine code directly
Memory Garbage collector frees objects You allocate and free
Array bounds Checked; throws IndexOutOfRangeException Not checked; may corrupt memory
Typical languages C#, F#, Visual Basic C, C++, assembly
Crossing over Use P/Invoke or COM interop to call unmanaged code Can host the CLR to run managed code

You can see the "managed" guarantees in action: an out-of-range array access is caught as an exception instead of silently reading someone else's memory.

C# The runtime protects managed code

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Calling unmanaged libraries from C# is covered in interop and unsafe code.

Assemblies and metadata

An assembly is the unit of deployment: a .dll or .exe that holds compiled IL. Inside an assembly are:

  1. A manifest: the assembly's name, version, culture, and the other assemblies it depends on.
  2. Metadata: a complete description of every type, member and attribute in the file.
  3. IL code: the compiled method bodies.
  4. Resources (optional): images, strings and other embedded files.

Metadata is why reflection works: the runtime can list a type's methods without any source code, and tools such as IDEs and decompilers read the same data. It is also what lets one language use another language's compiled library.

C# Reading an assembly's manifest and metadata

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

An assembly can be private (shipped next to your app) or shared through a package. In older .NET Framework the Global Assembly Cache (GAC) held machine-wide shared assemblies; modern .NET deliberately avoids it, and each app carries the versions it was tested with. You can also mark an assembly with a strong name (a cryptographic key pair) so that two libraries with the same simple name cannot be confused, though in modern .NET the main use is compatibility with old code.

For the visibility rules between assemblies see access modifiers, where internal means "visible inside this assembly".

.NET Framework, .NET Core and .NET

The product names are confusing because the platform was rebuilt halfway through its life. The timeline:

timeline
    title From .NET Framework to .NET 10
    2002 : .NET Framework 1.0 (Windows only)
    2016 : .NET Core 1.0 (open source, cross-platform)
    2019 : .NET Core 3.1 and the last .NET Framework, 4.8
    2020 : .NET 5 (the "Core" name dropped, a single .NET)
    2021 - 2025 : .NET 6, 7, 8, 9, 10 (yearly releases, LTS every even number)

.NET Framework is Windows-only and in maintenance mode; all new work targets modern .NET.

.NET Framework .NET (formerly .NET Core)
Platforms Windows only Windows, Linux, macOS, containers, mobile
Source Mostly closed Open source on GitHub
Installed Part of Windows, machine-wide Side by side; each app can pick its version
Development Security fixes only Active; new C# versions ship here
Newest C# C# 7.3 officially C# 14 with .NET 10

Two more names help to read documentation. .NET Standard was an interface that let one library run on both Framework and Core; with .NET 5+ unified, you can now target a recent .NET version directly. And the BCL (Base Class Library) is the common set of types in System.* that every .NET app can use, for example List<T>, File and HttpClient.

C# Which runtime am I on?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The runtime reports as ".NET 10.0.x", and the operating system on our server is Linux, which would have been impossible for .NET Framework.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the Common Type System (CTS) define?

    The CTS describes classes, value types, reference types, interfaces, inheritance rules and the built-in types that every .NET language shares.
  2. 2. Why would you mark a library [assembly: CLSCompliant(true)]?

    CLS compliance keeps public members within the common subset of features, such as no unsigned integers in public signatures and no names that differ only by case.
  3. 3. Which statement about managed code is correct?

    Managed code runs under the runtime, which supplies memory management, type safety and bounds checks. Other languages such as F# are managed too, and the compiler emits IL, not machine code.
  4. 4. Which runtime is Windows-only and receives only security fixes?

    .NET Framework runs only on Windows and is in maintenance mode. Modern .NET is cross-platform and receives new features.
  5. 5. What does the metadata in an assembly make possible?

    Metadata describes every type and member in the assembly, so tools and other languages can inspect and use them without source code.

Summary

  • The CTS defines the types all .NET languages share; C# keywords such as int are aliases for CTS types such as System.Int32.
  • The CLS is the common subset of the CTS; stay inside it in public libraries to be usable from every .NET language.
  • Managed code runs under the CLR with garbage collection, bounds checks and type safety; unmanaged code does not.
  • An assembly bundles a manifest, metadata and IL; metadata powers reflection and cross-language calls.
  • .NET Framework is the old Windows-only platform; .NET Core became plain ".NET" from version 5, and .NET 10 is the current LTS.

Next: C# syntax basics.