Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Passing arguments by default is ByRef instead of ByVal

Tags:

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();         ...      } 
like image 527
Nime Cloud Avatar asked Mar 15 '12 09:03

Nime Cloud


People also ask

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

What is C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

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)
like image 77
Jon Skeet Avatar answered Oct 09 '22 13:10

Jon Skeet