Home »
C#.Net »
C#.Net find output programs
C#.Net find output programs (Parameter Passing) | set 1
Find the output of C#.Net programs | Parameter Passing | Set 1: Enhance the knowledge of C#.Net Parameter Passing concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 07, 2021
Question 1:
using System;
namespace Demo
{
class Program
{
public static void SWAP(int &A,int &B)
{
int C=0;
C = A;
A = B;
B = C;
}
//Entry point of the program
static void Main(string[] args)
{
int X = 10;
int Y = 20;
SWAP(X,Y);
Console.WriteLine(X+","+Y);
}
}
}
Output:
main.cs(7,36): error CS1001: Unexpected symbol `&', expecting identifier
main.cs(7,43): error CS1001: Unexpected symbol `&', expecting identifier
Explanation:
The above program will generate syntax errors because in the above program we defined a static method SWAP() to swap two argument values, but we used '&', which is not correct way to use pass by reference mechanism for parameter passing into a method.
public static void SWAP(int &A,int &B)
The out and ref keywords are used for pass by reference parameter passing in C#.
Question 2:
using System;
namespace Demo
{
class Program
{
public static void SWAP(ref int A, ref int B)
{
int C = 0;
C = A;
A = B;
B = C;
}
//Entry point of the program
static void Main(string[] args)
{
int X = 10;
int Y = 20;
SWAP(ref X, ref Y);
Console.WriteLine(X + "," + Y);
}
}
}
Output:
20,10
Press any key to continue . . .
Explanation:
In the above program, we defined a static method SWAP() to swap two argument values, here we used the ref keyword for pass by reference parameter passing. If we use the ref keyword with parameters then changes made in the called method will be reflected in the calling method.
In the Main() method we declared two local variables X and Y initialized with 10 and 20 respectively, then values of variables get swapped in the SWAP() method then the values of X and Y will be 20 and 10 respectively.
Question 3:
using System;
namespace Demo
{
class Program
{
public static void method(out int A)
{
A = 20;
}
//Entry point of the program
static void Main(string[] args)
{
int A=10;
method(out A);
Console.WriteLine(A);
}
}
}
Output:
20
Press any key to continue . . .
Explanation:
In the above method, we created a class Program that contains a static method method() that takes a Output argument A and assigned value 20 to it. In the Main() method we created a local variable A initialized with 10. Then we pass variable A as an output parameter then 20 is assigned to the variable A inside method() method and then finally the value of A that is 20 is printed on the console screen.
Question 4:
using System;
namespace Demo
{
class Program
{
public static void method(ref int A)
{
A = 20;
}
//Entry point of the program
static void Main(string[] args)
{
int A;
method(ref A);
Console.WriteLine(A);
}
}
}
Output:
main.cs(18,24): error CS0165: Use of unassigned local variable `A'
Explanation:
The above program will generate syntax error because we cannot pass the un-assigned ref parameter into method.
Question 5:
using System;
namespace Demo
{
class Program
{
public static void method(out int A)
{
A = 20;
}
//Entry point of the program
static void Main(string[] args)
{
int A;
method(out A);
Console.WriteLine(A);
}
}
}
Output:
20
Press any key to continue . . .
Explanation:
In the above method, we created a class Program that contains a static method method() that takes a Output argument A and assigned value 20 to it. In the Main() method we created an un-initialized local variable A Then we pass variable A as an output parameter then 20 is assigned to variable A inside method() method and then finally the value of A that is 20 is printed on the console screen.