---
# System prepended metadata

title: mod04
tags: [C#]

---

```csharp
//if
DateTime d = DateTime.Now.AddHours(-5);
string greeting;
if (d.Hour>11)
{
    greeting = "午安";
}
else
{
    greeting = "早安";
}
Console.WriteLine(greeting);
=====================================
//?:
DateTime d = DateTime.Now.AddHours(-5);
string greeting= d.Hour > 11? "午安" : "早安";          
Console.WriteLine(greeting);
=====================================
//if pattern_matching
object obj = 100;
object obj = "100";
Console.WriteLine((int)obj+200);

object obj = 100;
object obj = "100";
if (obj is int)
{
    Console.WriteLine((int)obj + 200);
}

object obj = 100;
object obj = "100";
if (obj is int i)
{
    Console.WriteLine(i + 200);
}
//===============================
//Console.ReadLine
Console.Write("請輸入咖啡的大小 (L/M/S):");
string s = Console.ReadLine();
Console.WriteLine(s==string.Empty);
Console.WriteLine(s==null);//ctrl+z
Console.WriteLine(s=="L");
if (s != null)
{
    Console.WriteLine(s.ToUpper() == "L");
}

//  Console.WriteLine(s?.ToUpper() == "L");
===============================
//switch
Console.Write("請輸入咖啡的大小 (L/M/S):");
string s = Console.ReadLine();
string result;
switch (s?.ToUpper())
{
    case "L":
        result = "大杯";
        break;
    case "M":
        result = "中杯";
        break;
    case "S":
        result = "小杯";
        break;
    default:
        result = "輸入錯誤";
        break;

}
Console.WriteLine(result);
 ===============================
//goto case
Console.Write("請輸入咖啡的大小 (L/M/S):");
string s = Console.ReadLine();
string result=string.Empty;
switch (s?.ToUpper())
{
     case "L":
         result = "大杯";
         break;
         goto case "M";
     case "M":
         result += "中杯"; //result=result+"中杯"
         break;
     case "S":
         result = "小杯";
         break;
     default:
         result = "輸入錯誤";
         break;

 }
 Console.WriteLine(result);
//===============================
//goto label
   begin://label

   Console.Write("請輸入咖啡的大小 (L/M/S):");
   string s = Console.ReadLine();
   string result ;
   switch (s?.ToUpper())
   {
       case "L":
           result = "大杯";
           break; 
       case "M":
           result = "中杯"; 
           break;
       case "S":
           result = "小杯";
           break;
       default:
           goto begin;
           result = "輸入錯誤";
           break;

   }
   Console.WriteLine(result);
     //==================================
  //case
  DateTime now = DateTime.Now.AddMonths(1);
  DateTime now = DateTime.Now;
  string result;
  switch (now.Month)
  {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
          result = "現在是大月";
          break;
      case 2:
          result = "現在是二月";
          break;
      default:
          result = "現在是小月";
          break;
  }
  Console.WriteLine(result);
      //======================================
    //switch pattern matching
    object o = true;
    object o = 100;
    object o = "aaa";
    object o = "bbb";
    object o = DateTime.Now;

    string result;
    switch (o)
    {
        case int i:
result = "整數";
break;

        case string s when s.StartsWith("a"):
result = "a開頭的字串";
break;
        case string s:
result = s.ToUpper();
break;
        case DateTime d:
result = d.ToShortDateString();
break;
        default:
result = "無法處理";
break;
    }
    Console.WriteLine(result);
    //=========================
    //for
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine(i);
    }
    //=========================
    //var
    int i = 10;
    var j = 20;
    var d = DateTime.Now;
    Console.WriteLine(d.ToShortDateString());
    j = "xxx"; //x
    dynamic m = 30;
    dynamic d2=DateTime.Now;
    d2 = "xxx";
    Console.WriteLine(d2.toShortDateString());
    // ==================
     //do
     string msg;
     do
     {
         Console.Write("請輸入:");
         msg = Console.ReadLine();
         Console.WriteLine($"你輸入的是: {msg}");
     } while (msg != "quit");
      //==================
     ///while
     Random rnd = new Random();
     for (int i = 0; i < 50; i++)
     {
         Console.WriteLine(rnd.Next(1,10));
     }

     int i = 0;
     int j = 0;
     Console.WriteLine($"{++i},{j++}");
     Console.WriteLine($"{i},{j}");

     Random random = new Random();
     int count = 0;
     int data = 0;
     while (data != 7)
     {
         data = random.Next(1, 11);
         Console.WriteLine($"{++count}==>{data}");
     }
     Console.WriteLine($"第{count}次時中了幸運7!!");
    //==================
        //break
    string msg;
    do
    {
        Console.Write("請輸入:");
        msg = Console.ReadLine();
        if (msg == "quit") break;
        Console.WriteLine($"你輸入的是: {msg}");
    } while (true);
    //==================
    /continue
    int[] numbers = { 1, 3, 4, 0, 8, };
    for (int i = 0; i < numbers.Length; i++)
    {
        if (numbers[i] == 0) continue;
        Console.WriteLine(numbers[i]);
    }
        }
    }
/
