Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass primitive types by reference in C#?

Tags:

c#

.net

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

like image 744
Acaz Souza Avatar asked Aug 19 '11 13:08

Acaz Souza


People also ask

Do primitive types have references?

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.

Is pass by reference possible in C?

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.

How do you pass a primitive data type as a reference in Java?

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.

How are primitive datatypes passed to functions?

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.


5 Answers

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?

like image 101
Eric Lippert Avatar answered Oct 18 '22 02:10

Eric Lippert


Sure you can:

public void MyFunction(ref int a)
{
}
like image 31
Seva Alekseyev Avatar answered Oct 18 '22 02:10

Seva Alekseyev


Just use the ref or out parameter modifier.

void myMethod(ref int myValue) ...

ref is 2 way. out is 1 way.

like image 45
Daniel A. White Avatar answered Oct 18 '22 00:10

Daniel A. White


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);
}
like image 29
Daniel Avatar answered Oct 18 '22 00:10

Daniel


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.

like image 40
Jonathan Dickinson Avatar answered Oct 18 '22 02:10

Jonathan Dickinson