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:
The derived class:
Everything works well without any warnings. Rider told me the
LocalDbExtension
constructor can be converted into primary constructor, and helped me executed the conversion. Now it looks like this:
Now, when I build the project, compiler tells me:
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.
18 replies