Skip to content

Improve in parameter modifier example with meaningful struct-based demonstration #47134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot 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?

…ed demonstration

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@Copilot 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
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.
2 participants