> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C# Named Tuple Elements

Named tuple elements are compile-time constructs in C# that assign semantic identifiers to the fields of a `System.ValueTuple` structure. They replace the default `Item1`, `Item2`, ..., `ItemN` field names with user-defined aliases, providing stronger typing semantics at the source-code level without altering the underlying runtime type.

## Syntax and Initialization

Named elements can be defined either on the left-hand side (type declaration) or the right-hand side (value initialization) of an assignment.

```csharp theme={"dark"}
// Explicitly typed declaration (Left-hand side)
(int Id, string Username) user = (1, "admin");

// Implicitly typed declaration with named values (Right-hand side)
var configuration = (Port: 8080, Protocol: "HTTPS");

// Mixed declaration (Left-hand side names take precedence)
(int Code, string Message) response = (Id: 404, Text: "Not Found");
```

Starting with C# 7.1, the compiler supports **tuple projection initializers**, which automatically infer the element names from the variables or properties used to initialize the tuple.

```csharp theme={"dark"}
int count = 10;
string status = "Pending";

// The compiler infers the names 'count' and 'status'
var operation = (count, status); 

Console.WriteLine(operation.count); // Outputs: 10
```

## Compiler Mechanics and Metadata

Named tuples are syntactic sugar. At the Common Language Runtime (CLR) level, named tuples do not exist; they are compiled down to standard `System.ValueTuple<T1, T2, ...>` structs.

The compiler handles the translation of your custom names to the underlying `ItemN` fields. Because of this, the default `ItemN` fields remain accessible even when custom names are provided.

```csharp theme={"dark"}
var point = (X: 15, Y: 30);

// Both access the exact same underlying memory location
Console.WriteLine(point.X);     // Outputs: 15
Console.WriteLine(point.Item1); // Outputs: 15
```

When a named tuple is returned from a method or exposed as a property, the compiler preserves the element names across assembly boundaries by applying the `[TupleElementNames]` attribute to the member in the emitted Intermediate Language (IL) metadata.

## Assignment and Type Compatibility

Tuple element names are not part of the tuple's strict type identity. Type compatibility during assignment is determined exclusively by the **arity** (number of elements) and the **sequence of types**. Element names are entirely ignored during assignment.

```csharp theme={"dark"}
(int Alpha, int Beta) tupleA = (10, 20);
(int Gamma, int Delta) tupleB = (50, 60);

// Valid assignment: Arity (2) and types (int, int) match exactly.
tupleA = tupleB;

// tupleA.Alpha is now 50. The name 'Gamma' is dropped.
```

## Equality and Comparison

Because element names are erased at runtime, the equality operators (`==` and `!=`) evaluate tuples based on positional values, bypassing the names completely.

```csharp theme={"dark"}
var source = (A: 1, B: 2);
var target = (X: 1, Y: 2);

// Evaluates to true. The compiler compares Item1 to Item1, and Item2 to Item2.
bool isMatch = source == target; 
```

## Deconstruction

When deconstructing a named tuple into separate variables, the extraction is strictly positional. The names of the tuple elements do not need to match the names of the target variables.

```csharp theme={"dark"}
var dimensions = (Width: 1920, Height: 1080);

// Positional deconstruction: 'Width' goes to 'x', 'Height' goes to 'y'
(int x, int y) = dimensions;
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
