Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 8, 2025

Fixes #25422

Problem

The current example for the in parameter modifier uses a simple int parameter, which doesn't effectively demonstrate the purpose and benefits of the in modifier. As pointed out in the issue:

  • Without the in keyword, the value would still be 44 (since int is a value type)
  • The example doesn't show why you'd use in in the first place
  • It only demonstrates that you can't modify the parameter (via commented code)

Solution

Replaced the inadequate int example with a comprehensive struct-based demonstration that addresses all concerns:

Before (Problematic):

int readonlyArgument = 44;
InArgExample(readonlyArgument);
Console.WriteLine(readonlyArgument);     // value is still 44

void InArgExample(in int number)
{
    // Uncomment the following line to see error CS8331
    //number = 19;
}

After (Improved):

var largeStruct = new LargeStruct { Value1 = 42, Value2 = 3.14, Value3 = "Hello" };

// Using 'in' avoids copying the large struct and prevents modification
ProcessLargeStruct(in largeStruct);
Console.WriteLine($"Original value unchanged: {largeStruct.Value1}");

// Without 'in', the struct would be copied (less efficient for large structs)
ProcessLargeStructByValue(largeStruct);
Console.WriteLine($"Original value still unchanged: {largeStruct.Value1}");

void ProcessLargeStruct(in LargeStruct data)
{
    // Can read the values
    Console.WriteLine($"Processing: {data.Value1}, {data.Value2}, {data.Value3}");
    
    // Uncomment the following line to see error CS8331
    // data.Value1 = 99; // Compilation error: cannot assign to 'in' parameter
}

void ProcessLargeStructByValue(LargeStruct data)
{
    // This method receives a copy of the struct
    Console.WriteLine($"Processing copy: {data.Value1}, {data.Value2}, {data.Value3}");
    
    // Modifying the copy doesn't affect the original
    data.Value1 = 99;
}

What the New Example Demonstrates

  1. Why use in: Performance benefits when passing large structs (avoids copying)
  2. Realistic scenario: Processing data without needing to modify it
  3. Clear comparison: Shows both in parameter and regular parameter methods side-by-side
  4. Immutability: Demonstrates that in parameters cannot be modified
  5. Educational value: Enhanced comments explain the behavior and benefits

Changes Made

  • Updated FirstInExample() method in RefParameterModifier.cs with meaningful struct-based example
  • Added LargeStruct definition with multiple fields to demonstrate performance benefits
  • Enhanced comments explaining the purpose and benefits of in modifier
  • Maintained backward compatibility with existing documentation structure

The example now clearly shows why developers would choose to use the in modifier and provides a practical, educational demonstration of its benefits.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@BillWagner
Copy link
Member

@copilot Please start working on this again.

@BillWagner
Copy link
Member

@copilot Can you start working on this again?

@BillWagner
Copy link
Member

@copilot Can you start working on this again?

Copilot AI changed the title [WIP] Example seems incorrect. Improve in parameter modifier example with meaningful struct-based demonstration Jul 10, 2025
Copilot finished work on behalf of BillWagner July 10, 2025 20:42
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ready. I'll mark it ready for final review.

@BillWagner BillWagner marked this pull request as ready for review July 14, 2025 19:02
@BillWagner BillWagner requested a review from a team as a code owner July 14, 2025 19:02
@BillWagner BillWagner requested a review from IEvangelist July 14, 2025 19:02
@BillWagner
Copy link
Member

The snippets failure is related to a preview feature.

@BillWagner BillWagner merged commit b30aef3 into main Jul 15, 2025
20 of 22 checks passed
@BillWagner BillWagner deleted the copilot/fix-25422 branch July 15, 2025 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dotnet-csharp/svc lang-reference/subsvc okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Example seems incorrect.

3 participants