I know complex types are passed by Reference in C# and primitive types are passed by Value. Can I pass primitive types by reference in C#?
Update:
Thanks for answers, but my example is?
void test(object x) {
}
long y = 1;
test(ref y);
This throw this exception: The 'ref' argument type doesn´t match parameter type
The primitive types are boolean , byte , char , short , int , long , float and double . All other types are reference types, so classes, which specify the types of objects, are reference types. A primitive-type variable can store exactly one value of its declared type at a time.
Passing by by reference refers to a method of passing the address of an argument in the calling function to a corresponding parameter in the called function. In C, the corresponding parameter in the called function must be declared as a pointer type.
In Java, it is not possible to pass primitives by reference. To emulate this, you must pass a reference to an instance of a mutable wrapper class.
Passing primitive types in function: Primitive types are always passed by value. These are immutable. This means that even if we change the value in the function, the original value will not change. There are a total of five primitive data types in JavaScript.
There are a couple of different questions here.
Can I pass primitive types by reference in C#?
First off, let's make sure that the jargon is correct here. It is unclear what you mean by a "primitive type". Do you mean a "built into the runtime" type like int or long? Do you mean any value type whether it is built-in or user-defined?
I'm going to assume that your question actually is
Can I pass value types by reference in C#?
Value types are called value types because they are passed by value. Reference types are called reference types because they are passed by reference. So it would seem that the answer is by definition, no.
However, it's not quite so straightforward as that.
First, you can turn an instance of value type into an instance of reference type by boxing it:
decimal d = 123.4m; // 128 bit immutable decimal structure
object o1 = d; // 32/64 bit reference to 128 bit decimal
object o2 = o1; // refers to the same decimal
M(o2); // passes a reference to the decimal.
o2 = 456.78m; // does NOT change d or o1
Second, you can turn an instance of value type into a reference by making an array:
decimal[] ds1 = new decimal[1] { 123.4m };
decimal[] ds2 = ds1;
ds2[0] = 456.7m; // does change ds1[0]; ds1 and ds2 refer to the same array
Third, you can pass a reference to a variable (not a value -- a variable) using the "ref" keyword:
decimal d = 123.4m;
M(ref d);
...
void M(ref decimal x)
{ // x and d refer to the same variable now; a change to one changes the other
Attempting to pass a ref long to a method that takes a ref object causes a compilation error: The 'ref' argument type doesn´t match parameter type
Correct. The type of the variables on both sides must match exactly. See this question for details:
Why doesn't 'ref' and 'out' support polymorphism?
Sure you can:
public void MyFunction(ref int a)
{
}
Just use the ref
or out
parameter modifier.
void myMethod(ref int myValue) ...
ref
is 2 way.
out
is 1 way.
You can using the ref
word next to the parameter in the function definition.
public void func(ref int a)
{
//do stuff
}
public void doStuf()
{
int a = 5;
func(ref a);
}
Post-update you can pass boxed values around by reference.
static void Main(string[] args)
{
var value = 10;
var boxed = (object)value;
TakesValueByRef(ref boxed);
value = (int)boxed;
// value now contains 5.
}
static void TakesValueByRef(ref object value)
{
value = 5;
}
Just keep in mind it's terrible practice, and risky (due to cast exceptions) to do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With