Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing overload resolution behavior with params object[] parameter when passing int[] vs string[] (or array vs collection expression) #76854

Open
Youssef1313 opened this issue Jan 22, 2025 · 0 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@Youssef1313
Copy link
Member

Youssef1313 commented Jan 22, 2025

Two scenarios I found confusing:

Scenario 1

using System;

// Regular overload resolution works for both
var x = new MyAttribute(new int[] { 0, 1 }); // Okay
var y = new MyAttribute(new string[] { "Hello", "World" }); // Okay
var z = new MyAttribute([ "Hello", "World" ]); // Okay

[My(new int[] { 0, 1 })] // Okay
[My(new string[] { "Hello", "World" })] // Error
[My([ "Hello", "World" ])] // Okay - what?
public class C
{
    
}

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class MyAttribute : Attribute
{
    public MyAttribute(params object[] o)
    {
    }
}

Not sure if it's expected and why would "string" be very different from "int", and why collection expression is different. Both string and int are convertible to object. So to me feels like either both should error or both should be good. The former is a big breaking change.

Scenario 2

using System;

// Binds to object overload
var x = new MyAttribute(new int[] { 0, 1 });

// Binds to object[] overload. I don't see why they are different.
// This is just regular overload resolution, unrelated to attributes.
var y = new MyAttribute(new string[] { "Hello", "World" });

// Binds to object[] overload the same as array creation.
var z = new MyAttribute(["Hello", "World"]);

[My(new int[] { 0, 1 })] // Binds to object overload.
[My([0, 1])] // Binds to object[] overload
[My(new string[] { "Hello", "World" })] // Error. Should it bind to `object` overload?
[My([ "Hello", "World" ])] // Okay.. binds to object[] overload
public class C
{
    
}

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class MyAttribute : Attribute
{
    public MyAttribute(params object[] o)
    {
    }

    public MyAttribute(object o)
    {
    }
}
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 22, 2025
@Youssef1313 Youssef1313 changed the title Attribute constructor with params object[] parameter works when passing int[] but not string[] Confusing (unexpected?) overload resolution behavior with params object[] parameter when passing int[] vs string[] Jan 22, 2025
@Youssef1313 Youssef1313 changed the title Confusing (unexpected?) overload resolution behavior with params object[] parameter when passing int[] vs string[] Confusing (unexpected?) overload resolution behavior with params object[] parameter when passing int[] vs string[] (or array vs collection expression) Jan 22, 2025
@Youssef1313 Youssef1313 changed the title Confusing (unexpected?) overload resolution behavior with params object[] parameter when passing int[] vs string[] (or array vs collection expression) Confusing overload resolution behavior with params object[] parameter when passing int[] vs string[] (or array vs collection expression) Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

1 participant