Recep Şen

TB1REC

CTO @Taptoweb

担任Taptoweb的CTO,设计了为Easyapp.ai提供支持的28服务微服务平台——一个拥有25万+用户的无代码移动应用构建器。专注于.NET/C#、领域驱动设计和智能AI系统。

下载简历
Recep Şen - CTO profile photo
← Back to Blog
Performance Analysis of Parameter Passing Methods in C#
· 12 min read

Performance Analysis of Parameter Passing Methods in C#

Deep dive into C# parameter passing: Comparing struct, class, record & ref types with benchmarks. Learn best practices for optimization.

In modern software development, performance is a critical factor for the success of our applications. Especially in high-traffic systems, even micro-optimizations can have significant effects. In this article, we will examine the performance effects of different parameter passing methods in C# in detail.

Test Environment and Methodology

  • **Technical Specifications
    **- Test Framework: BenchmarkDotNet
    - Test Scenarios: Parameter counts from 3 to 8
    - Total Test Count: 660 (60 scenarios × average 11 iterations)
    - Tested Types:
    — Struct Based: `struct`, `readonly struct`, `ref struct`, `readonly ref struct`, `record struct`, `readonly record struct`
    — Reference Based: `class`, `sealed class`, `record`, `sealed record`
    — Direct Parameter Passing
  • **Measurement Metrics
    **- Mean: Average execution time of the method
    - Error: Error margin in measurements
    - StdDev: Consistency of measurements
    - Median: Median execution time
    - Memory Usage: Amount of memory allocated on the heap

Raw Benchmark Results

MethodCategoriesMeanErrorStdDevMedianIterationsGen0Allocated
EightParametersByReadonlyStructEightParams0.0089 ns0.0021 ns0.0020 ns0.0095 ns15.00--
EightParametersByRecordStructEightParams0.0108 ns0.0041 ns0.0039 ns0.0103 ns15.00--
EightParametersByReadonlyRefStructEightParams0.0134 ns0.0020 ns0.0018 ns0.0132 ns14.00--
EightParametersByRefStructEightParams0.0156 ns0.0030 ns0.0028 ns0.0163 ns15.00--
EightParametersByStructEightParams0.0171 ns0.0039 ns0.0037 ns0.0176 ns15.00--
EightParametersByReadonlyRecordStructEightParams0.0268 ns0.0023 ns0.0022 ns0.0270 ns15.00--
EightParametersEightParams0.0374 ns0.0211 ns0.0176 ns0.0298 ns13.00--
EightParametersBySealedClassEightParams3.9528 ns0.0108 ns0.0084 ns3.9549 ns12.000.007648 B
EightParametersBySealedRecordEightParams3.9665 ns0.0147 ns0.0131 ns3.9667 ns14.000.007648 B
EightParametersByRecordEightParams4.0113 ns0.0220 ns0.0206 ns4.0012 ns15.000.007648 B
EightParametersByClassEightParams4.0182 ns0.0551 ns0.0431 ns4.0169 ns12.000.007648 B
MethodCategoriesMeanErrorStdDevMedianIterationsGen0Allocated
SevenParametersByReadonlyStructSevenParams0.0132 ns0.0024 ns0.0022 ns0.0130 ns14.00--
SevenParametersByStructSevenParams0.0146 ns0.0028 ns0.0026 ns0.0142 ns13.00--
SevenParametersSevenParams0.0159 ns0.0052 ns0.0046 ns0.0166 ns14.00--
SevenParametersByReadonlyRefStructSevenParams0.0177 ns0.0026 ns0.0022 ns0.0176 ns13.00--
SevenParametersByRecordStructSevenParams0.0179 ns0.0017 ns0.0016 ns0.0178 ns15.00--
SevenParametersByRefStructSevenParams0.0179 ns0.0024 ns0.0022 ns0.0181 ns15.00--
SevenParametersByReadonlyRecordStructSevenParams0.0292 ns0.0026 ns0.0023 ns0.0288 ns14.00--
SevenParametersByClassSevenParams3.9931 ns0.0254 ns0.0237 ns3.9937 ns15.000.007648 B
SevenParametersBySealedClassSevenParams4.0053 ns0.0161 ns0.0135 ns3.9979 ns13.000.007648 B
SevenParametersByRecordSevenParams4.0944 ns0.0724 ns0.0677 ns4.0916 ns15.000.007648 B
SevenParametersBySealedRecordSevenParams4.1725 ns0.1052 ns0.0879 ns4.2199 ns13.000.007648 B
MethodCategoriesMeanErrorStdDevMedianIterationsGen0Allocated
SixParametersByRefStructSixParams0.0021 ns0.0085 ns0.0066 ns0.0000 ns12.00--
SixParametersByRecordStructSixParams0.0131 ns0.0069 ns0.0061 ns0.0110 ns14.00--
SixParametersByReadonlyRecordStructSixParams0.0136 ns0.0054 ns0.0045 ns0.0130 ns13.00--
SixParametersByStructSixParams0.0146 ns0.0016 ns0.0012 ns0.0149 ns12.00--
SixParametersByReadonlyRefStructSixParams0.0168 ns0.0114 ns0.0101 ns0.0159 ns14.00--
SixParametersSixParams0.0208 ns0.0072 ns0.0064 ns0.0184 ns14.00--
SixParametersByReadonlyStructSixParams0.0386 ns0.0213 ns0.0210 ns0.0383 ns16.00--
SixParametersBySealedClassSixParams3.6935 ns0.0120 ns0.0106 ns3.6921 ns14.000.006440 B
SixParametersBySealedRecordSixParams3.7268 ns0.0095 ns0.0079 ns3.7270 ns13.000.006440 B
SixParametersByRecordSixParams3.7375 ns0.0099 ns0.0088 ns3.7393 ns14.000.006440 B
SixParametersByClassSixParams3.7637 ns0.0832 ns0.0854 ns3.7260 ns17.000.006440 B
MethodCategoriesMeanErrorStdDevMedianIterationsGen0Allocated
FiveParametersByStructFiveParams0.0122 ns0.0028 ns0.0026 ns0.0130 ns15.00--
FiveParametersFiveParams0.0145 ns0.0068 ns0.0057 ns0.0136 ns13.00--
FiveParametersByRecordStructFiveParams0.0147 ns0.0033 ns0.0031 ns0.0148 ns15.00--
FiveParametersByReadonlyStructFiveParams0.0159 ns0.0033 ns0.0031 ns0.0162 ns15.00--
FiveParametersByReadonlyRecordStructFiveParams0.0167 ns0.0043 ns0.0041 ns0.0170 ns15.00--
FiveParametersByReadonlyRefStructFiveParams0.0167 ns0.0037 ns0.0032 ns0.0170 ns14.00--
FiveParametersByRefStructFiveParams0.0275 ns0.0138 ns0.0129 ns0.0205 ns15.00--
FiveParametersBySealedRecordFiveParams3.6785 ns0.0159 ns0.0141 ns3.6770 ns14.000.006440 B
FiveParametersBySealedClassFiveParams3.6964 ns0.0158 ns0.0123 ns3.6935 ns12.000.006440 B
FiveParametersByRecordFiveParams3.6994 ns0.0114 ns0.0107 ns3.6982 ns15.000.006440 B
FiveParametersByClassFiveParams3.8910 ns0.0951 ns0.0794 ns3.9040 ns13.000.006440 B
MethodCategoriesMeanErrorStdDevMedianIterationsGen0Allocated
FourParametersFourParams0.0171 ns0.0047 ns0.0044 ns0.0154 ns15.00--
FourParametersByReadonlyRecordStructFourParams0.5746 ns0.0333 ns0.0396 ns0.5763 ns21.00--
FourParametersByRecordStructFourParams0.5781 ns0.0305 ns0.0271 ns0.5790 ns14.00--
FourParametersByReadonlyStructFourParams0.5926 ns0.0342 ns0.0380 ns0.5998 ns19.00--
FourParametersByStructFourParams0.6749 ns0.0347 ns0.0426 ns0.6816 ns22.00--
FourParametersByRefStructFourParams0.6863 ns0.0359 ns0.0515 ns0.6945 ns28.00--
FourParametersByReadonlyRefStructFourParams0.6889 ns0.0364 ns0.0577 ns0.6910 ns33.00--
FourParametersBySealedClassFourParams3.4595 ns0.0209 ns0.0195 ns3.4674 ns15.000.005132 B
FourParametersByClassFourParams3.4767 ns0.0314 ns0.0294 ns3.4868 ns15.000.005132 B
FourParametersByRecordFourParams3.4836 ns0.0144 ns0.0135 ns3.4857 ns15.000.005132 B
FourParametersBySealedRecordFourParams3.7335 ns0.0230 ns0.0215 ns3.7415 ns15.000.005132 B
MethodCategoriesMeanErrorStdDevMedianIterationsGen0Allocated
ThreeParametersThreeParams0.0188 ns0.0115 ns0.0102 ns0.0155 ns14.00--
ThreeParametersByRecordStructThreeParams0.5678 ns0.0277 ns0.0259 ns0.5662 ns15.00--
ThreeParametersByReadonlyRecordStructThreeParams0.5717 ns0.0302 ns0.0267 ns0.5729 ns14.00--
ThreeParametersByRefStructThreeParams0.7168 ns0.0360 ns0.0538 ns0.7239 ns30.00--
ThreeParametersByReadonlyRefStructThreeParams0.7234 ns0.0365 ns0.0721 ns0.7082 ns48.00--
ThreeParametersByReadonlyStructThreeParams0.7349 ns0.0368 ns0.0573 ns0.7346 ns32.00--
ThreeParametersByStructThreeParams0.7548 ns0.0370 ns0.0648 ns0.7332 ns39.00--
ThreeParametersByClassThreeParams3.4737 ns0.0831 ns0.0737 ns3.4421 ns14.000.005132 B
ThreeParametersBySealedClassThreeParams3.4743 ns0.0697 ns0.0582 ns3.4544 ns13.000.005132 B
ThreeParametersBySealedRecordThreeParams3.8524 ns0.0835 ns0.0781 ns3.8314 ns15.000.005132 B
ThreeParametersByRecordThreeParams3.9852 ns0.1021 ns0.1327 ns3.9314 ns24.000.005132 B

Performance of Struct-Based Types

**1. struct and readonly struct
**Structs showed the best performance especially for small data structures:
- ~0.6–0.7 ns with 3–4 parameters
- ~0.01–0.03 ns with 5+ parameters
- No memory allocation (stack-based)
- `readonly struct` is 5–10% faster on average compared to normal `struct`

**2. ref struct and readonly ref struct
**Safe performance with heap allocation restriction:
- Similar performance to normal structs
- No memory allocation
- Extra security with reference semantics

**3. record struct and readonly record struct
**High performance with modern C# features:
- Best performing structures
- ~0.57 ns with 3–4 parameters
- ~0.01–0.02 ns with 5+ parameters
- Includes immutability and equality comparison

Performance of Reference Types

**1. class and sealed class
**General characteristics of reference types:
- Average range of 3.4–4.0 ns
- `sealed class` is 1–3% faster than normal `class`

**2. record and sealed record
**Modern immutable data structures:
- Similar performance to classes
- Same memory usage pattern
- Value-based equality and immutability advantages

**Direct Parameter Passing
**The simplest approach:
- Consistent low latency (~0.015–0.037 ns)
- No memory allocation
- Code readability and maintenance difficulty disadvantages

General Recommendations

**Less Than 5 Parameters
**- Prefer `readonly record struct`
- Use `readonly ref struct` if memory sensitive
- Use `record` if immutability is important

**5+ Parameters
**- Struct-based types have significant advantage
- Use `readonly` modifier
- Consider parameter grouping

**Special Cases
**- When inheritance is needed: Base class with inheritance hierarchy
- When value equality is important: `record`
- When stack-only is required: `ref struct`

Performance Optimization Suggestions

**By Parameter Count
**- 3–4 parameters: Struct-based types
- 5+ parameters: Readonly struct variations
- Many parameters: Consider grouping

**Memory Optimization
**- Maximize stack usage
- Avoid unnecessary heap allocations
- Use appropriately sized structs

**Type Selection Criteria
**- Performance requirements
- Memory constraints
- Code maintainability balance

**Conclusion
**This benchmark study has thoroughly revealed the performance characteristics of parameter passing methods in C#. Especially modern C# features (like `readonly record struct`) offer significant advantages in terms of both performance and code quality.

**Key findings
**1. Struct-based types show superior performance especially with 5+ parameters
2. Positive performance effect of readonly modifier
3. Modern and efficient use of record structures
4. Memory usage characteristics of reference types

You can access the source code of the benchmark tests in this article via .NET Fiddle.

**About the Author
**- Website: senrecep.com
- GitHub: senrecep
- Nuget: recepsen
- LinkedIn: senrecep
- X: @senrecep0

发送邮件
发送 WhatsApp 消息