Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which data types are reference types by default?

Tags:

c#

So while I was testing a method I made with a DataRow parameter, I sent in an argument and modified it inside the method. After that, my original DataRow changed to whatever it ended up being after the method. I realized this was because DataRow is a "reference type", which is new to me.

I now know that I can use "ref" or "out" before other data types when using methods so that the same effect as the DataRow example would happen. But my question is, which data types are reference types by default? I don't want to be caught off guard when I pass another kind of data type into a method and having the original value change.

like image 431
Kevin Cho Avatar asked Dec 21 '25 11:12

Kevin Cho


2 Answers

But my question is, which data types are reference types by default?

Anything that is defined as a class is a reference type, as well as a variable referencing an interface or delegate, or a variable declared dynamic. See Reference Types for details. You can change members of a class within a method without passing by ref or out. That being said, you can't change the reference itself - so the variable will always point to the same instance after a method call as before the method call unless you pass it via out or ref.

If it's a struct, it will be a value type.

like image 100
Reed Copsey Avatar answered Dec 23 '25 01:12

Reed Copsey


A better question is what types are not reference types.

MSDN documentation

  • Structs
  • Enumerations

Structs fall into these categories:

  • Numeric types: Integral types, Floating-point types and decimal
  • bool
  • User defined structs
like image 44
Chuck Conway Avatar answered Dec 22 '25 23:12

Chuck Conway