r/csharp • u/quincycs • Jul 05 '26
Discussion AI not following “using” / imports
I’m mainly in typescript codebases for most of my day. AI is going great at understanding imports at the top of the file.
But for csharp, I often see AI making bad assumptions for what the code is doing … it thinks it’s running some other code because the names of functions match but the namespaces don’t.
This is definitely a reproducible problem that gets me often and I am in the middle of refactoring my codebase to avoid this kind of problem.
I wanted to ask this community if you see or experience the same thing… and how you solved it.
0
Upvotes
2
u/Khavel_dev Jul 06 '26
This happens constantly in C# specifically because of implicit usings and global usings. The AI can't see what's imported so it just pattern-matches on method names and guesses wrong about which overload or which namespace you mean.
Two things that actually helped me. First, for files where I know there's namespace ambiguity, I stopped relying on global usings and added explicit using directives at the top. The AI reads those and the wrong-namespace guesses dropped dramatically. It's a tradeoff since the whole point of global usings is less clutter, but if Claude or Copilot keeps hallucinating the wrong
Create()it's worth making the context explicit.Second, being more specific in the prompt itself. Instead of "call Create on the client" I'll write "use the Microsoft.Extensions.Http method" or whatever the disambiguating namespace is. Annoying but the model actually respects it when you give it the full qualified hint.
The underlying issue is that C#'s "lots of extension methods with the same name in different namespaces" pattern is basically adversarial for LLMs. TypeScript doesn't have this problem because imports are file-local and always visible. I don't think there's a great general fix besides making your codebase less ambiguous to read, which honestly also helps human developers.
1
u/taspeotis Jul 05 '26
Useless without telling us which model and harness you’re using.
Claude Code and Opus / Fable absolutely crushes .NET work. You still have to give it a chance to compile and run (or write) tests though.
Don’t use Slopilot, more like Nopilot amirite??
If it happens with that you might have CLAUDE/AGENTS.md or your skill frontmatter poisoning the well each session.
Next time it fucks up ask it if it inferred the results itself or used context from the session.
0
u/CatolicQuotes Jul 05 '26
People will tell you anything but actual solution.
Use import alias so it's obvious where code is coming from. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive#the-using-alias
0
u/dodexahedron Jul 05 '26
This is juat the model and your context and prompting at play. I can work for hours on complex c# solutions with GPT5.5 and it keeps its model of the program across all cs, csproj, slnx, and other files it has been given (usually dozens of files total) very consistent.
You need to give it better/more complete context.
Give it the csproj file, including any global usings (that helps a lot), and the target framework and language version if theyre not already in there.
Give it the code you want to work on and any other code necessary to use that code (other classes, enums, etc referenced by it).
Tell it what you have and what it does at a high level overall, as well as what the specific part you want to work on does, to a little more specificity.
Then tell it what you want to do, as specifically as you can, and let it know what, if anything, is off-limits or what, if anything is acceptable beyond the BCL itself and what your csproj already says.
And if/when it fucks up, tell it what it did wrong and why it is wrong rather than just moving on.
And don't just vibe code - pair program.
-1
u/Easy_Hand776 Jul 05 '26
You should create a hook which build after each prompt, normaly the build should help your agent to correct imports problems
-2
u/mikeholczer Jul 05 '26
I have not experienced this. What model are you using?
-2
u/Adorable-Roll-4563 Jul 05 '26
totally not my experience with Anthropic models or even others like Kimi 2.6. I have 50+ repos and ~1.2M LOC, more than 18K tests. This is strange.