© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
17 replies
Ge Xiaohe

✅ How to resolve CS9017 when trying passing captured arguments by primary constructor to base class?

I am using C# 12.

The base class:
public class DbExtension(IOptionsMonitor<AppConfig> appConfig, LogExtension logger, string connectionString)
{ ... }
public class DbExtension(IOptionsMonitor<AppConfig> appConfig, LogExtension logger, string connectionString)
{ ... }


The derived class:
public class LocalDbExtension : DbExtension
{
    private readonly IOptionsMonitor<AppConfig> _appConfig;

    public LocalDbExtension(IOptionsMonitor<AppConfig> appConfig, LogExtension logger, string connectionString)
        : base(appConfig, logger, connectionString)
    {
        _appConfig = appConfig;
    }

    ...
}
public class LocalDbExtension : DbExtension
{
    private readonly IOptionsMonitor<AppConfig> _appConfig;

    public LocalDbExtension(IOptionsMonitor<AppConfig> appConfig, LogExtension logger, string connectionString)
        : base(appConfig, logger, connectionString)
    {
        _appConfig = appConfig;
    }

    ...
}


Everything works well without any warnings. Rider told me the
LocalDbExtension
LocalDbExtension
constructor can be converted into primary constructor, and helped me executed the conversion. Now it looks like this:
public class LocalDbExtension(IOptionsMonitor<AppConfig> appConfig, LogExtension logger, string connectionString)
    : DbExtension(appConfig, logger, connectionString)
{ ... }
public class LocalDbExtension(IOptionsMonitor<AppConfig> appConfig, LogExtension logger, string connectionString)
    : DbExtension(appConfig, logger, connectionString)
{ ... }


Now, when I build the project, compiler tells me:
warning CS9107: Parameter `IOptionsMonitor<AppConfig> appConfig` is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.
warning CS9107: Parameter `IOptionsMonitor<AppConfig> appConfig` is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.


From the doc:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/constructor-errors#primary-constructor-syntax
CS9107 - Parameter is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well. This warning indicates that your code may be allocated two copies of a primary constructor parameter. Because the parameter is passed to the base class, the base class likely uses it. Because the derived class accesses it, it may have a second copy of the same parameter. That extra storage may not be intended.

I have no idea how to resolve this warning if keeping using primary constructor on this class.
Resolve errors related to constructor declarations
These compiler errors and warnings indicate violations when declaring constructors in classes or structs, including records. This article provides guidance on resolving those errors.
Resolve errors related to constructor declarations
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Primary Constructor
C#CC# / help
5mo ago
Init Timer in class with Primary Constructor?
C#CC# / help
7mo ago
Any way to inherit base abstract class' constructor?
C#CC# / help
2y ago
Converting the simple constructor to primary constructor
C#CC# / help
3y ago