0% found this document useful (0 votes)
16 views9 pages

Output

The document contains C# code examples demonstrating the use of 'ref', 'out', and 'params' keywords, showcasing how to manipulate values and perform calculations. It includes methods for incrementing values, adding numbers, finding maximum values, and processing collections with type checks and parsing. The output section details the results of various operations, illustrating the combined effects of these keywords in a programmatic context.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views9 pages

Output

The document contains C# code examples demonstrating the use of 'ref', 'out', and 'params' keywords, showcasing how to manipulate values and perform calculations. It includes methods for incrementing values, adding numbers, finding maximum values, and processing collections with type checks and parsing. The output section details the results of various operations, illustrating the combined effects of these keywords in a programmatic context.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

using System;

using System.Linq;

class Program
{
public static void Inc(ref int i)
{
i++;
}
public static void Add(int a, int b, out int sum, out int product)
{
sum = a + b;
product = a * b;
}
public static int MaxVal(params int[] numbers)
{
return numbers.Max();
}

static void Main(string[] args)


int x = 0;
Inc(ref x);
Console.WriteLine("Ref example: " + x);
int s, p;
Add(2, 3, out s, out p);
Console.WriteLine("Out example (Sum): " + s);
Console.WriteLine("Out example (Product): " + p);
int max = MaxVal(1, 46, 25, 22, 99);
Console.WriteLine("Params example (Max): " + max);
object o = "ABC";
if (o is string str)
{
Console.WriteLine("Is example: " + str); }
else
{
Console.WriteLine("Is example: Not a string");
}
object obj = "Hello World";
string str2 = obj as string;
if (str2 == null)
{
Console.WriteLine("As example: Not representable as string");
}
else
{
Console.WriteLine("As example: " + str2); }

object obj2 = 123;


string str3 = obj2 as string;
if (str3 == null)
{
Console.WriteLine("As example (wrong cast): Not representable
as string");
}
}
}

2.
using System;

class Program
{
// params + out
static void ProcessNumbers(out int sum, params int[] numbers)
{
sum = 0;
foreach (var n in numbers)
{
sum += n;
}
Console.WriteLine("Sum inside ProcessNumbers: " + sum);
}

// ref
static void DoubleValue(ref int x)
{
x *= 2;
}

static void Main()


{
int total;
ProcessNumbers(out total, 1, 2, 3); // params + out
Console.WriteLine("Total after ProcessNumbers: " + total);

int a = 5;
DoubleValue(ref a); // ref
Console.WriteLine("Value of a after DoubleValue: " + a);

object obj = "100";


// is + out (pattern matching + parsing)
if (obj is string s && int.TryParse(s, out int parsed))
{
Console.WriteLine("Parsed value: " + parsed);
}

object obj2 = 123;


string str = obj2 as string; // as
Console.WriteLine("obj2 as string: " + (str ?? "Casting failed"));

// Combination trick
object obj3 = 50;
int val = 10;
if (obj3 is int i)
{
DoubleValue(ref val); // ref inside condition
Console.WriteLine("val after ref in is block: " + val);
int sum2;
ProcessNumbers(out sum2, val, i); // params + out again
Console.WriteLine("sum2 in combination: " + sum2);
}
}
}

3.

using System;

class Program
{
// params + out
static void Process(out int sum, params int[] numbers)
{
sum = 0;
foreach (var n in numbers) sum += n;
Console.WriteLine("Inside Process: sum = " + sum);
}

// ref
static void Multiply(ref int x, int factor)
{
x *= factor;
}

static void Main()


{
int total;
Process(out total, 1, 2, 3, 4); // params + out
Console.WriteLine("Total after Process: " + total);

int a = 5;
Multiply(ref a, total); // ref + depends on previous total
Console.WriteLine("a after Multiply: " + a);

object obj = "250";


// is + out + inline parsing
if (obj is string s && int.TryParse(s, out int parsed))
{
Multiply(ref parsed, 2);
Console.WriteLine("Parsed * 2: " + parsed);
}

object obj2 = 123;


string str = obj2 as string ?? "DefaultString"; // as + null coalescing
Console.WriteLine("obj2 as string: " + str);

// Complex combination
object[] arr = { "10", 20, "30" };
int sum2 = 0;

foreach (var item in arr)


{
if (item is string st && int.TryParse(st, out int val))
{
Multiply(ref val, 3); // ref + is + out
sum2 += val;
Console.WriteLine("String item processed: " + val);
}
else if (item as int? is int n) // as + is
{
Multiply(ref n, 4); // ref inside is block
sum2 += n;
Console.WriteLine("Int item processed: " + n);
}
}

Process(out int finalSum, a, parsed, sum2); // params + out +


previous values
Console.WriteLine("Final combined sum: " + finalSum);
}
}

🔹 Output Step by Step:

1. Process(out total, 1,2,3,4) → sum = 10


o Prints: Inside Process: sum = 10
o total = 10 → Prints: Total after Process: 10
2. Multiply(ref a, total) → a = 5 × 10 = 50
o Prints: a after Multiply: 50
3. obj is string s && int.TryParse(s, out parsed) → parsed = 250
o Multiply by 2 → 250 × 2 = 500
o Prints: Parsed * 2: 500
4. obj2 as string ?? "DefaultString" → obj2 is int → null →
"DefaultString"
o Prints: obj2 as string: DefaultString
5. foreach(arr) → tricky:
o item = "10" → parsed 10 ×3 = 30 → sum2 = 30 → Prints:
String item processed: 30
o item = 20 → int? is int n → 20 ×4 = 80 → sum2 = 110 →
Prints: Int item processed: 80
o item = "30" → parsed 30 ×3 = 90 → sum2 = 200 → Prints:
String item processed: 90
6. Process(out finalSum, a, parsed, sum2) → Process(50, 500, 200) →
sum = 50+500+200=750
o Prints: Inside Process: sum = 750
o finalSum = 750 → Prints: Final combined sum: 750

✅ Full Output:

Inside Process: sum = 10


Total after Process: 10
a after Multiply: 50
Parsed * 2: 500
obj2 as string: DefaultString
String item processed: 30
Int item processed: 80
String item processed: 90
Inside Process: sum = 750
Final combined sum: 750

এই কোডের trickiness:

 সব keyword একই program এ ব্যবহার


 Nested is + out + ref
 as + ?? null coalescing
 params দিয়ে multiple values
 Sequential dependency (previous results affect next calculations)

You might also like