Click here to Skip to main content
14,580,589 members
Rate this:
Please Sign up or sign in to vote.
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);
}}
Posted
Updated 6-Oct-15 9:44am
v3
Comments
Maciej Los 6-Oct-15 15:46pm
   
What kind of method it is: "console.wriline"?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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);
  }
}
   
Comments
suresh92 6-Oct-15 15:45pm
   
ok i want hard answer..
CPallini 6-Oct-15 15:46pm
   
?
Maciej Los 6-Oct-15 15:47pm
   
The best explanation without any words.
5ed!
Maciej Los 6-Oct-15 15:48pm
   
:laugh:
It couldn't be more hard...
CPallini 6-Oct-15 15:49pm
   
Thank you, Maciej!
suresh92 6-Oct-15 15:52pm
   
ok dude thank u..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100