Skip to content

C# -> VB: Add Overloads when an inherited member has the same name - #1321

Open
Alexilia wants to merge 2 commits into
icsharpcode:masterfrom
Alexilia:fix/overloads-base-members
Open

Alexilia wants to merge 2 commits into
icsharpcode:masterfrom
Alexilia:fix/overloads-base-members

Conversation

@Alexilia

@Alexilia Alexilia commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A VB method or property without Overloads hides every inherited member of the same name, while C# only hides the same signature. The converter added Overloads only next to an override of the same name in the same type, so:

  • an IEqualityComparer<T>.Equals(T, T) hid Object.Equals(Object, Object), and x.Name == y.Name inside it (Equals(x.Name, y.Name)) failed with BC30311;
  • a derived indexer hid the base indexer;
  • a method in a derived interface hid the base interface's overload, and with Option Strict Off d.Run(1) silently called Run(String).

Solution

  • Overloads is added to methods, properties and indexers when an accessible member of the same kind and name (ignoring case) is inherited from a base class or base interface. Members of static classes (VB modules) are skipped, as Overloads isn't allowed there.
  • At least one test covering the code changed (methods, indexers, interfaces and the cases that must stay unchanged; a CS -> VB self-verifying test checks that the inherited overloads are still the ones called; no existing expectation changed)

Process

  • Attachment level to the code in this PR: high. It was written with Claude (Anthropic), then reviewed by me; I understand it and take responsibility for it as my own code.
  • LLM primarily used: Claude Opus 5.5

@GrahamTheCoder

GrahamTheCoder commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Thanks for the PR! I'm worried about:

  1. The performance on types with many members since it's N^2 in the number of members. Probably easily cached per visitor on first use with a Lazy which is likely enough.
  2. The case of an indexer which trips over an existing conversion oddity where the MetadataName isn't used. The best fix would be to improve VisitIndexerDeclaration to write the MetadataName. Can put it out of scope for this PR if you like since it's a shortcoming based on another issue rather than regression.

Note on out of scope cross cutting issue: We should possibly be using Microsoft.CodeAnalysis.CaseInsensitiveComparison.Equals in case it differs now or in future from OrdinalIgnoreCase comparisons.

Comment thread CodeConverter/VB/NodesVisitor.cs Outdated

/// <summary>
/// A VB method or property declared without Overloads, Overrides or Shadows hides every accessible inherited member
/// of the same name (case-insensitively, BC40003/BC40004), also in interfaces, whereas C# only hides the same signature.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably either remove mention of BC40004 or explain the reasoning around what happens, since this method implementation only deals with same kind. It's currently a bit at odds to mention it but then exclude that case without further explanation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed BC40004. The summary now says why only a member of the same kind counts: VB can't overload a property with a method or the reverse, so such a member stays hidden by name either way.

A VB method or property without Overloads hides every inherited member of the
same name, while C# only hides the same signature. The converter added
Overloads only next to an override of the same name in the same type, so:
- `Equals(Item x, Item y)` of an IEqualityComparer hid Object.Equals(Object,
  Object), and `x.Name == y.Name` converted to `Equals(x.Name, y.Name)` bound
  to the comparer's own method (BC30311);
- a derived indexer hid the base indexer (`this["a"]` failed to compile);
- in a derived interface, `Run(string)` hid `Run(int)` of the base interface,
  and with Option Strict Off `d.Run(1)` silently called `Run("1")`.
Overloads is now added to an ordinary method or a non-explicit property
(including indexers, compared by metadata name "Item") when an accessible
member of the same kind and name (ignoring case) is inherited from a base
class or, for an interface, a base interface. Members of a static class that
becomes a Module are skipped: a Module inherits nothing and doesn't allow
Overloads (BC36917).
- Cache the accessible inherited members of a type by name, so a type with
  many members reads its base types once instead of once per member.
- Compare names with CaseInsensitiveComparison, as VB does.
- Declare an indexer under its metadata name, so [IndexerName("Chars")]
  gives `Default Property Chars` rather than `Item`, and the Overloads check
  compares the names VB actually declares. Explicit implementations keep
  `Item`, bound through Implements.
- Explain why only an inherited member of the same kind counts.
@Alexilia
Alexilia force-pushed the fix/overloads-base-members branch from 55d6bf2 to 3829507 Compare November 24, 2026 20:16
@Alexilia

Copy link
Copy Markdown
Contributor Author

Thanks! All three are addressed in a separate commit (3829507):

  1. Performance: the accessible inherited members are now cached per type, in a dictionary on the visitor, as a lookup by name. Each member then does one lookup instead of scanning the base types.
  2. Indexer MetadataName: it turned out small, so it's included. VisitIndexerDeclaration now declares the default property under the indexer's metadata name: [IndexerName("Chars")] gives Default Property Chars instead of Item. So the Overloads check compares the names VB actually declares. Explicit implementations keep Item, because their metadata name is qualified and Implements does the binding. Tests: a conversion test and two self-verifying ones, one checking the base indexer is still reachable, one checking GetProperty("Value") finds the renamed property. The conversion test and the GetProperty one failed before; the first guards the Overloads case. One case is left as it was: a base and a derived indexer with different names, where VB's default property in the derived class still hides the base one.
  3. Switched to CaseInsensitiveComparison here (the lookup uses CaseInsensitiveComparison.Comparer).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants