什么是价值调用和ref调用? [英] what is call by value and call by ref?

查看:125
本文介绍了什么是价值调用和ref调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
按价值致电并按推荐致电

如何影响价值



hi In call by value and call by reference
how to effect values

using system;
class sample
{
private int a,b,c;
public void getdata(int a,int b)
{
this.a=a;
this.b=b;
}
public void caldisp()
{
int c;
this.c=a+b;
c=this.c;
console.wriline(a+""+b+""+c+""+this.c);
}}
class program
{
p.s.v.m()
{
console.write("enter 1st value");
int i=int.parse(console.readline());
console.write("enter 2nd value");
int j=int.parse(console.readline());
sample s=new sample();
s.getdata(i,j);
s.caldisp();
}}
call by reference
<pre lang="cs">using system;
class sample
{
private int a,b;
public void show(int x,int y,out int s,out int p)
{
a=x;
b=y;
s=a+b;
p=a*b;
}}
class program
{
p.s.v.m()
{
int i=18;int j==29;
int r1,r2;
sample s=new sample();
s.show(i,j,out r1,out r2)
console.write("sum is="+r1);
console.write("sum is="+r2);
}}

推荐答案

试试这个:

Try this:
using System;
public class Program
{
  static void by_value(int x)
  {
    x = 42;
  }

  static void by_reference(ref int x)
  {
    x = 42;
  }


  public static void Main()
  {
    int i = 0;
    by_value(i);
    Console.WriteLine("after by_value, i = {0}", i);
    by_reference(ref i);
    Console.WriteLine("after by_reference, i = {0}", i);
  }
}


这篇关于什么是价值调用和ref调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆