Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:19:34+00:00 2026-06-10T11:19:34+00:00

Why do I get the following to errors when I try to add ref

  • 0

Why do I get the following to errors when I try to add ref to an overloaded method’s parameter?

The best overloaded method match for
‘WindowsFormsApplication1.Form1.SearchProducts(int)’ has some invalid
arguments

Argument 1: cannot convert from ‘ref
System.Collections.Generic.List’ to ‘int’

Here’s some (simplified) code:

public virtual IList<int> SearchProducts(int categoryId)
{
    List<int> categoryIds = new List<int>();
    if (categoryId > 0)
        categoryIds.Add(categoryId);
    return SearchProducts(ref categoryIds);
}

public virtual IList<int> SearchProducts(ref IList<int> categoryIds)
{
    return new List<int>();
}

Edit:

Some of you asked me why I need ref in this case and the answer is that I probably don’t need it, because I can clear the list and add new elements (I don’t need to create a new reference). But the question is not about the fact that I need or don’t need ref, it’s about why I got the errors. And since I didn’t find an answer (after googling for little while) I thought the question was interesting and worth asking here. It seems that some of you don’t think it’s a good question and voted to close it down …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:19:35+00:00Added an answer on June 10, 2026 at 11:19 am

    When you pass an argument by reference, the compile-time type has to be the exact same type as the parameter type.

    Suppose the second method was written as:

    public virtual IList<int> SearchProducts(ref IList<int> categoryIds)
    {
        categoryIds = new int[10];
        return null;
    }
    

    That must compile, as int[] implements IList<int>. However, it would break type safety if the caller actually had a variable of type List<int>, which now had a reference to an int[]…

    You can fix this by making the declared type of categoryIds in the calling method IList<int> instead of List<int> – but I strongly suspect you don’t actually want to pass the argument by reference in the first place. It’s relatively rare to need to do so. How comfortable are you with C# parameter passing?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try to serialize class with protected members, I get the following errors:
If I try to execute the following code, I get the errors Msg 207,
I get following error message, when I try to run git rebase -i for
I get the following error when I try and execute the code down below.
I get the following error when I try to run sample example of Google
I get the following error message when I try the following: Dim XL As
I get the following error on my Cloud Foundry installation when I try to
I get the following error in the UDK Frontend when I try to make
When I try to I get the following error in SQL Server Management Studio:
When I try to start an IntentService over adb i get tho following error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.