---
# System prepended metadata

title: mod06
tags: [C#]

---

```csharp
//by value
//int i = 10;
//func1(i);
//Console.WriteLine(i);   // 10

//void func1(int j)
//{
//    j = 100;
//}
//==================
//by ref
//int i = 10;
//func2(ref i);
//Console.WriteLine(i);   // 11

//void func2(ref int j)
//{
//    j = j + 1;
//}
//==================
//out
func3(out int i);
Console.WriteLine(i); // 100

void func3(out int j)
{
    j = 100;
}
//==================
//tryparse
//string s = "1000";
//int i = int.Parse(s);
//Console.WriteLine(i + 500);

string s = "1000";
int.TryParse(s, out int i);
Console.WriteLine(i + 500);
//==================
//int[] numbers = { 1, 2, 3, 4 };
//func(numbers);
//void func(int[] ar)
//{
//    ar[0] = 1000;
//}
//Console.WriteLine(numbers[0]); //1000

//int[] numbers = { 1, 2, 3, 4 };
//func(ref numbers);
//void func(ref int[] ar)
//{
//    ar[0] = 1000;
//}
//Console.WriteLine(numbers[0]); //1000
//==================
//int[] numbers = { 1, 2, 3, 4 };
//func(numbers);
//void func(int[] ar)
//{
//    ar = new int[] { 100, 200, 300, 400 };
//}
//Console.WriteLine(numbers[0]); //1

int[] numbers = { 1, 2, 3, 4 };
func(ref numbers);
void func(ref int[] ar)
{
    ar=new int[] { 100, 200, 300, 400 };
}
Console.WriteLine(numbers[0]); //100

//====================
//optional parameter
Display(1);
Display(1,2);
Display(1,m:3);
void Display(int i, int j = 200, int m = 300)
{
    Console.WriteLine((i, j, m));
}
//====================
//param array
Console.WriteLine(Sum(1,3,4,7));
Console.WriteLine(Sum(new int[] {100,200,300}));
int Sum(params int[] numbers)
{
    return numbers.Sum();
}
//====================
//return tuple
var result = ParseInt("1000");
Console.WriteLine((result.Item1, result.Item2+500));
var result2 = ParseInt("a1000");
Console.WriteLine((result2.Item1, result2.Item2 + 500));
(bool, int) ParseInt(string s)
{
    return (int.TryParse(s,out int i),i);
}
//====================
//deconstructing
var (a,b) = ParseInt("1000");
Console.WriteLine((a, b));

(bool, int) ParseInt(string s)
{
    return (int.TryParse(s,out int i),i);
}
//====================
//expression-bodied function
//int result = Add(100, 50);
//Console.WriteLine(result);
//int Add(int i, int j)
//{
//    return i + j;
//}

int result = Add(100, 50);
Console.WriteLine(result);
int Add(int i, int j)=> i + j;
//====================
//xml documentation
int result = ComputeArea(100, 200);
Console.WriteLine(result);

/// <summary>
/// 計算面積
/// </summary>
/// <param name="width">寬</param>
/// <param name="height">高</param>
/// <returns>寬高相乘結果</returns>
static int ComputeArea(int width, int height) => width * height;

```