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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:32:37+00:00 2026-05-31T18:32:37+00:00

I know the default is ByVal in C#. I used same variable names in

  • 0

I know the default is ByVal in C#. I used same variable names in many places and then I noticed passed values change and come back. I think I knew scope mechanism of C# wrong. Here public license overrides the local license values. I know I can easily rename the variable names in conflict but I would like to learn the facts about scope.

  public static class LicenseWorks
  {
     public static void InsertLicense(License license)
     {
        license.registered = true;
        UpdateLicense(license);
     }
  }

  public partial class formMain : Form
  {
     License license;

     private void btnPay_Click(object sender, EventArgs e)
     {
          license.registered = false;
          LicenseWorks.InsertLicense(license);

          bool registered = license.registered; //Returns true!
      }
  }    

Update: I’ve added below as solution:

    public static void InsertLicense(License license)
    {

        license = license.Clone();
        ...
     }
  • 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-31T18:32:38+00:00Added an answer on May 31, 2026 at 6:32 pm

    The argument is being passed by value – but the argument isn’t an object, it’s a reference. That reference is being passed by value, but any changes made to the object via that reference will still be seen by the caller.

    This is very different to real pass by reference, where changes to the parameter itself such as this:

     public static void InsertLicense(ref License license)
     {
        // Change to the parameter itself, not the object it refers to!
        license = null;
     }
    

    Now if you call InsertLicense(ref foo), it will make foo null afterwards. Without the ref, it wouldn’t.

    For more information, see two articles I’ve written:

    • Parameter passing in C#
    • References and values (the differences between value types and reference types)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do you know the default isolation level of the transactions used in Django? Is
I would like to know if by default is Hashtable passed by reference or
Does anyone know the default values for the timeout of service endpoints in a
I know the default value for norm_type used when calling the function is 4,
I know that default WPF behavior is to render WPF controls and then on
I know that default constructors initialize objects to their default values, but how do
For any given type i want to know its default value. In C#, there
I know mongrel is the default server for script/server but when I do that
Anyone know how to turn off the default error page or where it's referenced??
By default nunit tests run alphabetically. Does anyone know of any way to set

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.