Skip to content

"Generate Equals" refactorings produce non-compiling code when using indexers #25690

@jhinder

Description

@jhinder

Version Used: VS 15.7 Preview 2 (Roslyn 2.8.0.62716, e205ae18)

Steps to Reproduce:

  1. Copy the class below into a C# source file.
  2. Invoke the Quick Fix menu at the marked location and select either "Generate Equals(object)…" or "Generate Equals and GetHashCode…".
  3. Make sure the indexer "this[int]" is selected in the list. Press OK.
class Repro
{
	public int this[int index]
	{
		get => index;
	}
	$$
}

Expected Behavior: The generated methods are syntactically valid. (Although I don't know how one could use an indexer to implement Equals or GetHashCode.)

Actual Behavior: The generated code looks something like this, which is syntactically invalid:

public override bool Equals(object obj)
{
	var repro = obj as Repro;
	return repro != null &&
		this[] == repro.this[]; // Emits four compiler errors and one warning
}

public override int GetHashCode()
{
	return -529683855 + this[].GetHashCode(); // Emits one compiler error
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions