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 383551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:17:37+00:00 2026-05-12T15:17:37+00:00

When the formal argument of a method is of type ‘object’ it is possible,

  • 0

When the formal argument of a method is of type ‘object’ it is possible, through inheritance, for the actual argument to be any object type. Once in the method the object can be cast to the type expected. All’s fine.

If, however, the signature of the method has a formal argument of ‘object’, using the ref keyword i.e. methodname(ref object), the compiler throws an error stating that:

“The best overloaded method match for ‘ByRefTest.Program.changeMeByRef(ref object)’ has some invalid arguments.
“Argument ‘1’: cannot convert from ‘ref ByRefTest.Person’ to ‘ref object'”

The difference between using or not using the ref keword when passing objects as parameters is explained very well in A. Friedman’s blog http://crazorsharp.blogspot.com/2009/07/passing-objects-using-ref-keywordwait.html , but why is it not possible to pass a custom type as an actual argument when the formal argument of type ‘object’ uses the ref keyword?

As an example:

class Program
{
    static void Main(string[] args)
    {
        Person p = new Person();

        changeMe(p); // compiles
        changeMeByRef(ref p); // throws error

        object pObject = (object)p;
        changeMeByRef(ref pObject); // compiles
    }


    public static void changeMeByRef(ref object obj)
    {
        Person p = (Person)obj;
    }

    public static void changeMe(object obj)
    {
        Person p = (Person)obj;

    }
}

public class Person
{
}

Thanks.

ps I just changed the signature to:

public static void changeMeByRef<T>(ref T obj) where T : Person

this compiles for changeMeByRef(ref p);

  • 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-05-12T15:17:38+00:00Added an answer on May 12, 2026 at 3:17 pm

    Because the method might change the parameter to have a value of different type, in violation of the type of the variable used for the argument. Simple example:

    public void Foo(ref object x)
    {
        x = "hello";
    }
    
    ...
    
    // This doesn't compile, fortunately.
    Stream y = null;
    Foo(ref y);
    

    What’s the value of y after the assignment in the method? It should be a string – but that violates the type of the variable. That would be a Very Bad Thing, completely violating type safety. This is the same kind of reason for the generic invariance of IList<T> even in .NET 4.0.

    Eric Lippert recently blogged about the invariance of ref in more detail.

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

Sidebar

Related Questions

Consider the method declaration: String.format(String, Object ...) The Object ... argument is just a
bsddb.db.DBInvalidArgError: (22, 'Invalid argument -- /dbs/supermodels.db: unexpected file type or format') Is this error
symbol.c: In function 'symbol_FPrint': symbol.c:1209: warning: format '%ld' expects type 'long int', but argument
Are there any formal recommendations on element casing in XML? I know XHTML uses
We have method that looks like this : public String getCommandString(Object...args) { return this.code
I'm getting the following error: The type arguments for method 'System.Web.Mvc.Html.DisplayExtensions.DisplayFor<TModel,TValue>(System.Web.Mvc.HtmlHelper<TModel>, System.Linq.Expressions.Expression<System.Func<TModel,TValue>>, string)' cannot
How print format string passed as argument ? example.cpp: #include <iostream> int main(int ac,
In formal specifications based on abstract algebraic types and equational theory you use formulas
Do you use a formal event to get people talking in your IT department?
background: - there are formal languages for expressing programming language valid lexicon and syntax

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.