-2

I was sticking on Unity for a while, without knowing much about Computer Science/Performance management.

Now, I took some C++ experience, with some concerns on IO/performance/etc some "Computer Engineering" stuffs.

Then I got back to my old Unity project, which had no concern about those thing at all, and found out that there is ref(or out) keyword for call-by-reference, as I could do in my C++ project.

Is it common to use ref(for call-by-reference purpose) of C# on 'business level'?(maybe game companies with Unity?)

9
  • stackoverflow.com/questions/20450888/using-ref-in-c-sharp according to a comment on this page, it's not common; which would be right? Commented Jul 23, 2020 at 6:56
  • Do you know the difference between a value and a reference type? Most things you pass in C# are already passed as a reference under the hood. Commented Jul 23, 2020 at 6:56
  • I supposed its how you look at it. every single C# feature is common and used extensively. I guess at this stage you should probably do some research on those particular language features Commented Jul 23, 2020 at 7:01
  • Is it common to use ref .. well for some reason it exists ;) Completely depends on the use case .. note that all class (reference) types are passed by reference anyway so the ref only makes a difference for structs (value types).... Commented Jul 23, 2020 at 7:08
  • Does this answer your question? What's the difference between the 'ref' and 'out' keywords? Commented Jul 23, 2020 at 7:10

1 Answer 1

3

If you come from C++ then you have this thought in your head that you need to pass references, because if you don't, the whole object will be copied and most likely that is bad (tm).

In C#, types are either reference types or value types. Reference types are passed by reference automatically. If you pass a class, it can be null, so it must already be a reference to the actual class instance (or in C++ terminology a "pointer", because references in c++ cannot be null).

All classes are reference types. You don't need ref or out for performance reasons ever. If you find yourself using ref or out for performance reasons on a struct, that should be your clue that it should be a class instead.

So no, using ref or out is not common in C# at all. It is only used sparingly, in the places where you would not get the desired result without it. Those keywords are not meant for performance considerations.

Sign up to request clarification or add additional context in comments.

15 Comments

I use it on numeric values or structs I have no access to so I can't convert them to classes e.g.
The whole point of a struct is that it is so small, that copying it is faster than the indirection via reference. If those structs come from a source you trust to make good software decisions, use them without ref/out. If you don't trust the source to make good software decisions, pick another source.
"Reference types are passed by reference automatically." Actually, that's not entirely accurate. Reference types are not passed at all - what's being passed is the reference itself, and it's being passed by value. For more information, read this.
I may have musunderstood the point of ref and out, but I thought they're not about performance or "having to copy the whole struct", but more about being able to change/set the value of a variable "outside" its original scope. So of course you could have a method int AddOne(int value) => value + 1; and use it like int i = 1; i = AddOne(i);. But you could also do it like void AddOne(ref int value) => value += 1; and use it like int i = 1; AddOne(ref i); in a more involved scenario, the latter could be more readable. And I like out very much in TryParse or TryGet situations.
@nvoigt Ok, I'll accept that explanation.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.