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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:36:21+00:00 2026-06-06T10:36:21+00:00

I’ve this question about pass some instances by ref or not: here is my

  • 0

I’ve this question about pass some instances by ref or not: here is my problem:

Case 1: simple var like int:

private void button2_Click(object sender, EventArgs e)
{
    int nTest = 10;

    testInt(nTest);
    MessageBox.Show(nTest.ToString());
    // this message show me 10

    testIntRef(ref nTest);
    MessageBox.Show(nTest.ToString());
    // this message show me 11
}

private void testInt(int nn)
{
    nn++;
}

private void testIntRef(ref int nn)
{
    nn++;
}

this is exactly what I think, if I use the ref, the parameter is passed by reference, so if is changed, when I exit from the function, the value is changed…

Case 2: class:

// simple class to understand the reference..
public class cTest
{
    int nTest;
    public cTest()
    {
        setTest(0);
    }

    public void setTest(int n)
    {
        nTest = n;
    }

    public int getTest()
    {
        return nTest;
    }
}

// my main code
private void button3_Click(object sender, EventArgs e)
{
    cTest tt = new cTest();
    tt.setTest(2);

    testClass(tt);

    // I expect that the message shows me 2, 'cause testClass
    // doesn't have (ref cTest test)
    MessageBox.Show(tt.getTest().ToString());
}

private void testClass(cTest test)
{
    test.setTest(55);
}

and, as written in the comment on the code, I don’t have passed my cTest as reference, but the result is the same, the message show me 55 and not 2..

How can I pass a class without reference?

  • 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-06T10:36:22+00:00Added an answer on June 6, 2026 at 10:36 am

    How can I pass a class without reference?

    You can’t.

    You can clone that instance and send it, but it will still be sent by ref…

    • class – Reference type
    • struct – Value type.

    Reading:

    • Article about passing variables in C#

    • Wikipedia about Objects copy– shallow copy + deep copy.

    Quoting Jon Skeet C# in depth second edition:

    MYTH #3: “OBJECTS ARE PASSED BY REFERENCE IN C# BY DEFAULT”

    This is probably the most widely propagated myth. Again, the people who make this
    claim often (though not always) know how C# actually behaves, but they don’t know
    what “pass by reference” really means. Unfortunately, this is confusing for people who
    do know what it means. The formal definition of pass by reference is relatively complicated,
    involving l-values and similar computer science terminology, but the important
    thing is that if you pass a variable by reference, the method you’re calling can change
    the value of the caller’s variable by changing its parameter value. Now remember that the
    value of a reference type variable is the reference, not the object itself. You can change
    the contents of the object that a parameter refers to without the parameter itself being
    passed by reference.
    For instance, the following method changes the contents of the
    StringBuilder object in question, but the caller’s expression will still refer to the
    same object as before:

    void AppendHello(StringBuilder builder)
    {
        builder.Append("hello");
    }
    

    When this method is called, the parameter value (a reference to a StringBuilder) is
    passed by value. If I were to change the value of the builder variable within the
    method—for example, with the statement builder = null;—that change wouldn’t be
    seen by the caller, contrary to the myth
    .

    C# in depth Value types and reference types page 46

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

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.