tags:

views:

84

answers:

2

As in c # to create and then call the function? In C + + do so:

int func (int value) 
{ 
   value +=2;
}

But as is done in c #?

+4  A: 

Here's the same function translated in C#:

public void Func(ref int value) 
{
   // Parameter is passed by reference so any modification
   // to the value will propagate to the caller
   value +=2;
}

And call it like this:

int value = 5;
Func(ref value); // value is now 7

UPDATE:

If as an alternative you don't want to reflect the modifications made to the value parameter outside the function you could declare it like this:

public void Func(int value) 
{ 
   // Parameter is passed by value so any modification
   // to the value will not propagate to the caller
   value +=2;
}

And call like this:

int value = 5;
Func(value); // value is still 5
Darin Dimitrov
That may be the intended goal, but that's not what the OP's function is doing. `value` isn't passed by reference in his code.
Blindy
the OP's snippet wouldn't even compile though would it?
fearofawhackplanet
@Blindy, I agree with you and updated my answer to reflect your suggestion.
Darin Dimitrov
Pretty sure it would.
Blindy
A: 

Considering your example is wrong... well, anyway.

public void func(ref int value)
{
  value+=2;
}
Femaref
This doesn't compile in C#.
Darin Dimitrov
I am aware of that. His example doesn't make sense either.
Femaref