开发者

functions in C# [closed]

开发者 https://www.devze.com 2022-12-30 07:25 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

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 开发者_如何学Cc #?


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


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

public void func(ref int value)
{
  value+=2;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消