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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:12:58+00:00 2026-06-01T08:12:58+00:00

I always thought that a method parameter with a class type is passed as

  • 0

I always thought that a method parameter with a class type is passed as a reference parameter by default. Apparently that is not always the case. Consider these unit tests in C# (using MSTest).

[TestClass]
public class Sandbox
{
    private class TestRefClass
    {
        public int TestInt { get; set; }
    }

    private void TestDefaultMethod(TestRefClass testClass)
    {
        testClass.TestInt = 1;
    }

    private void TestAssignmentMethod(TestRefClass testClass)
    {
        testClass = new TestRefClass() { TestInt = 1 };
    }

    private void TestAssignmentRefMethod(ref TestRefClass testClass)
    {
        testClass = new TestRefClass() { TestInt = 1 };
    }

    [TestMethod]
    public void DefaultTest()
    {
        var testObj = new TestRefClass() { TestInt = 0 };
        TestDefaultMethod(testObj);
        Assert.IsTrue(testObj.TestInt == 1);
    }

    [TestMethod]
    public void AssignmentTest()
    {
        var testObj = new TestRefClass() { TestInt = 0 };
        TestAssignmentMethod(testObj);
        Assert.IsTrue(testObj.TestInt == 1);
    }

    [TestMethod]
    public void AssignmentRefTest()
    {
        var testObj = new TestRefClass() { TestInt = 0 };
        TestAssignmentRefMethod(ref testObj);
        Assert.IsTrue(testObj.TestInt == 1);
    }
}

The results are that AssignmentTest() fails and the other two test methods pass. I assume the issue is that assigning a new instance to the testClass parameter breaks the parameter reference, but somehow explicitly adding the ref keyword fixes this.

Can anyone give a good, detailed explanation of whats going on here? I’m mainly just trying to expand my knowledge of C#; I don’t have any specific scenario I’m trying to solve…

  • 1 1 Answer
  • 1 View
  • 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-01T08:12:59+00:00Added an answer on June 1, 2026 at 8:12 am

    The thing that is nearly always forgotten is that a class isn’t passed by reference, the reference to the class is passed by value.

    This is important. Instead of copying the entire class (pass by value in the stereotypical sense), the reference to that class (I’m trying to avoid saying “pointer”) is copied. This is 4 or 8 bytes; much more palatable than copying the whole class and in effect means the class is passed “by reference”.

    At this point, the method has it’s own copy of the reference to the class. Assignment to that reference is scoped within the method (the method re-assigned only its own copy of the reference).

    Dereferencing that reference (as in, talking to class members) would work as you’d expect: you’d see the underlying class unless you change it to look at a new instance (which is what you do in your failing test).

    Using the ref keyword is effectively passing the reference itself by reference (pointer to a pointer sort of thing).

    As always, Jon Skeet has provided a very well written overview:

    http://www.yoda.arachsys.com/csharp/parameters.html

    Pay attention to the “Reference parameters” part:

    Reference parameters don’t pass the values of the variables used in
    the function member invocation – they use the variables themselves.

    If the method assigns something to a ref reference, then the caller’s copy is also affected (as you have observed) because they are looking at the same reference to an instance in memory (as opposed to each having their own copy).

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

Sidebar

Related Questions

In C#, I have always thought that non-primitive variables were passed by reference and
I always thought that objects where always passed as reference in C# and that
I have always thought that the .equals() method in java should be overridden to
I always thought that internal class has access to all data in its external
I always thought that if you didn't implement a heartbeat, there was no way
I always thought that parentheses improved readability, but in my textbook there is a
I always thought that when you dropped a control onto an .aspx page that
I always thought that *&p = p = &*p in C. I tried this
I always thought that LIMIT in a query selects only between numbers I set.
I always thought that Core Animation performs animations on the background. When I run

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.