Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# pass by value/ref?

Tags:

Common question but I could use an "english" explanation.

Is it like Java where

Cat myCat 

actually is a pointer to Cat?

Should I really create copy constructors in C#?


I understand we are passing by value, but now my question is are we passing by pointer value or full copy of the object?

If it's the latter, isn't that too expensive performance/memory wise? Is that when you have to use the ref keyword?

like image 791
masfenix Avatar asked Jan 12 '09 20:01

masfenix


People also ask

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 ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

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.


2 Answers

As @rstevens answered, if it is a class, myCat is a reference. But if you pass myCat to a method call, then the reference itself is passed by value - i.e. the parameter itself will reference the same object, but it's a completely new reference, so if you assign it to null, or create a new object, the old myCat reference will still point to the original object.

SomeMethod(myCat);  void SomeMethod(Cat cat) {     cat.Miau(); //will make the original myCat object to miau     cat = null; //only cat is set to null, myCat still points to the original object } 

Jon Skeet has a good article about it.

like image 65
Sunny Milenov Avatar answered Oct 04 '22 19:10

Sunny Milenov


Remember that a pointer is not exactly the same as a reference, but you can just about think of it that way if you want.

I swear I saw another SO question on this not 10 minutes ago, but I can't find the link now. In the other question I saw, they were talking about passing arguments by ref vs by value, and it came down to this:

By default in .Net, you don't pass objects by reference. You pass references to objects by value.

The difference is subtle but important, especially if, for example, you want to assign to your passed object in the method.

like image 36
Joel Coehoorn Avatar answered Oct 04 '22 21:10

Joel Coehoorn