🔧 ProgrammierungRobot Fleet Management Software: A Complete Guide(16.09.2026 um 15:24 Uhr)
🕵️ SicherheitslückenKnown MCP Vulnerabilities and How an MCP Gateway Blocks Them(16.09.2026 um 15:21 Uhr)
🔧 ProgrammierungSimba 3.2 Frontier TTS for $10/1M Characters(16.09.2026 um 15:35 Uhr)
🔧 ProgrammierungAdd casting to a custom HTML5 video player without breaking it(16.09.2026 um 15:39 Uhr)
🔧 AI Nachrichten SK Hynix reportedly in talks with Intel to build memory chips in US(16.09.2026 um 15:23 Uhr)
🔧 AI Nachrichten What AI Can Teach Us About Being Human(16.09.2026 um 15:37 Uhr)
🔧 ProgrammierungRobot Fleet Management Software: A Complete Guide(16.09.2026 um 15:24 Uhr)
🕵️ SicherheitslückenKnown MCP Vulnerabilities and How an MCP Gateway Blocks Them(16.09.2026 um 15:21 Uhr)
🔧 ProgrammierungSimba 3.2 Frontier TTS for $10/1M Characters(16.09.2026 um 15:35 Uhr)
🔧 ProgrammierungAdd casting to a custom HTML5 video player without breaking it(16.09.2026 um 15:39 Uhr)
🔧 AI Nachrichten SK Hynix reportedly in talks with Intel to build memory chips in US(16.09.2026 um 15:23 Uhr)
🔧 AI Nachrichten What AI Can Teach Us About Being Human(16.09.2026 um 15:37 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

Understanding How the .NET Compiler Handles Generics

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Generics are one of the most powerful features in .NET, allowing developers to create reusable and type-safe code. While they simplify development for us, the .NET compiler handles some complex tasks behind the scenes to ensure everything works efficiently. Let’s break it down into simple terms with examples.









What Does the Compiler Do?



When you define a generic class like List<T>, the compiler doesn’t know in advance what type T will be. This creates challenges in determining how much memory to allocate for fields and methods that depend on T. Here's how the compiler tackles this:






Example of a Generic Class






CODE
public class GenericHolder<T>
{
public T Value { get; set; }
}






In this generic class, the type of the Value property depends on T. The compiler doesn’t know its size or layout until you instantiate the class.









Handling Reference vs. Value Types






1. Reference Types



Reference types, such as string or object, always have the same size, which is the size of a pointer. On a 64-bit system, this size is 8 bytes.




CODE
var stringHolder = new GenericHolder<string> { Value = "Hello" };
Console.WriteLine(stringHolder.Value); // Outputs: Hello






Here’s what happens:




  • The Value property holds a pointer (8 bytes) to the memory location where the actual string "Hello" is stored.

  • The string data itself is stored elsewhere in memory.






2. Value Types



Value types, such as int or DateTime, can have different sizes. For example:





  • int: 4 bytes


  • DateTime: 8 bytes




CODE
var intHolder = new GenericHolder<int> { Value = 42 };
Console.WriteLine(intHolder.Value); // Outputs: 42

var dateHolder = new GenericHolder<DateTime> { Value = DateTime.Now };
Console.WriteLine(dateHolder.Value); // Outputs: Current date and time






Here’s what happens:




  • The Value property directly stores the int or DateTime value.

  • The compiler ensures the correct memory layout for the specific value type.









How Does Memory Allocation Work?



To understand memory allocation, let’s use the List<T> class as an example.






Example: List of Reference Types (string)






CODE
var stringList = new List<string> { "Hello", "World" };
Console.WriteLine(stringList[0]); // Outputs: Hello







  • Each element in stringList is a pointer (8 bytes) to a string object.

  • For 100 items, the list allocates (100 \times 8 = 800) bytes for the references.

  • The memory for the actual string data is stored separately and depends on the string content.






Example: List of Value Types (int)






CODE
var intList = new List<int> { 1, 2, 3 };
Console.WriteLine(intList[0]); // Outputs: 1







  • Each element in intList directly stores the integer value (4 bytes each).

  • For 100 items, the list allocates (100 \times 4 = 400) bytes.









Reusing Type Layouts



The runtime reuses type layouts to optimize memory usage. For example:




  • A List<double> and a List<DateTime> share the same layout because double and DateTime are both 8 bytes.






Example: Reused Layouts






CODE
var dateList = new List<DateTime> { DateTime.Now, DateTime.Today };
var doubleList = new List<double> { 1.1, 2.2, 3.3 };

Console.WriteLine(dateList[0]); // Outputs: Current date and time
Console.WriteLine(doubleList[0]); // Outputs: 1.1






Even though the data is different, the memory layout for both lists is identical because the size of each element (8 bytes) is the same.









Why Does This Matter?



For developers, understanding these details can help you write efficient code. However, the compiler and runtime handle the complexities, allowing you to focus on writing logic without worrying about memory layouts.






Summary





  1. Reference Types:




    • Stored as pointers (e.g., 8 bytes in a 64-bit system).

    • Actual data is stored elsewhere.




  2. Value Types:




    • Size varies (e.g., int is 4 bytes, DateTime is 8 bytes).

    • Stored directly in the list or class.




  3. Efficient Reuse:




    • The runtime reuses generic type layouts where possible, saving memory.





Generics in .NET combine flexibility with performance. Whether you’re working with reference or value types, you can trust the compiler and runtime to optimize your code.

Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
2 Quellen
Apache Syncope Vulnerabilities Allow Attackers to Execute Malicious Code and Bypass Controls
1 Quelle
SK Hynix reportedly in talks with Intel to build memory chips in US
1 Quelle
What AI Can Teach Us About Being Human
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Understanding How the .NET Compiler Handles Generics

Thematisch verwandte Begriffe: Understanding, Compiler, Handles, Generics · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...