Untitled
unknown
csharp
10 months ago
2.1 kB
7
Indexable
using System;
namespace MyApp
{
internal class Program
{
//****************************(Functions)********************************
// Pass by value: The parameter is passed by copying the value.
// Modifying 'num' inside the method won't affect the original variable.
static int IncrementByValue(int num)
{
num++;
return num;
}
// Pass by reference: The parameter is passed as a reference.
// Changes made to 'num' directly affect the original variable.
static int IncrementWithRef(ref int num)
{
num++;
return num; // The variable must be initialized before being passed to ref.
}
// Pass with out parameter: The parameter is passed uninitialized.
// The method must assign a value before using the parameter.
static int IncrementWithOut(out int num)
{
num = 10; // Assigning a value to the out parameter.
num++;
return num;
}
//*******************************************************************
static void Main(string[] args)
{
//****************************(Main Code)********************************
int a = 10; // 'a' is initialized before being passed to the ref parameter.
IncrementWithRef(ref a); // Passing by reference using ref.
int c; // 'c' is declared but not neccessery initialized.
IncrementWithOut(out c); // Passing by reference using out (requires assignment inside the method).
int b = 10; // 'b' is initialized.
IncrementByValue(b); // Passing by value.
// Output the results to the console
Console.WriteLine(a); // Output: 11
Console.WriteLine(c); // Output: 11
Console.WriteLine(b); // Output: 10
//*******************************************************************
}
}
}
Editor is loading...
Leave a Comment