Flow של תוכנית C#

Console Input/Output

קלט נתונים מהמשתמש ב-console.

static void Main(string[] args) {
    Console.WriteLine("Enter you name:");
    string name = Console.ReadLine();
    Console.WriteLine("Your name is: " + name);
    Console.ReadLine();
}

קריאת המשתנה באותה השורה.

static void Main(string[] args) {
    Console.Write("Enter you name: ");
    string name = Console.ReadLine();
    Console.WriteLine("Your name is: " + name);
    Console.ReadLine();
}

If statements

static void Main(string[] args) {
    Console.Write("Enter you name: ");
    string name = Console.ReadLine();
    Console.WriteLine("Your name is: " + name);

    Console.Write("Enter you age: ");
    int age = Convert.ToInt32(Console.ReadLine());

    if(age > 18) {
        Console.WriteLine("You are an adult");
    } else if (age > 3 && age < 10) {
        Console.WriteLine("You have time to grow up");
    }
    Console.ReadLine();
}

Switch statements

static void Main(string[] args) {
    Console.Write("Enter the day of the week: ");
    int day = Convert.ToInt32(Console.ReadLine());

    switch (day) {
        case 1: Console.WriteLine("Sun");
            break;
        case 2:
        case 3:
            Console.WriteLine("Mon or Tue");
            break;
        default: Console.WriteLine("Wrong day");
            break;
    }
    Console.ReadLine();
}

For loops

static void Main(string[] args) {
    for(int i = 0; i < 5; i++) {
        Console.WriteLine("i is " + i);
    }
    Console.ReadLine();
}

While loops

static void Main(string[] args) {
    int i = 0;

    while(i < 10) {
        Console.WriteLine(i);
        i++;
    }

    do{
    } while (i < 10);
            
    Console.ReadLine();
}

Conditional operator

static void Main(string[] args) {
    int age = -10;

    string result = age >= 0 ? "Valid" : "Invalid";
    Console.WriteLine(result);
            
    Console.ReadLine();
}

Numeric formatting

using System.Globalization;

static void Main(string[] args) {
    double value = 1000D / 12.34D;

    Console.WriteLine(value);
    // Two values in the string. For value number 0, the first value will be taken,
    // and for value number 1, the second value will be taken.
    Console.WriteLine(string.Format("{0} {1}", value, 1000));
    // All the value before the decimal point will be displayed and
    // only 2 numbers after the decimal point.
    Console.WriteLine(string.Format("{0:0.00}", value));
    Console.WriteLine(value.ToString("C", CultureInfo.CreateSpecificCulture("en-US")));
            
    Console.ReadLine();
}

TryParse function

static void Main(string[] args) {
    Console.Write("Enter a number: ");
    int num = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine(num);
            
    Console.ReadLine();
}

תוכנית פשוטה כמו זו למעלה תעבוד מצויין כל עוד הכניסו לקלט מספר בלבד. אם יש אותיות בקלט, והתוכנית מנסה להמיר אותן למספר, נקבל שגיאה והתוכנית תעוף.

על מנת למנוע את זה נשתמש בפונקציה TryParse. הפונקציה מקבלת כמשתנה ראשון מחרוזת שאותה היא מנסה להמיר למספר. אם יש הצלחה, המספר יאוחסן במשתנה השני של הפונקציה ויהיה אפשר להשתמש בו.

static void Main(string[] args) {
    Console.Write("Enter a number: ");
    int.TryParse(Console.ReadLine(), out int num);
    Console.WriteLine(num);
            
    Console.ReadLine();
}

הפונקציה TryParse מחזירה ערך בוליאני של true אם הפעולה הצליחה ו-false אם הפעולה לא הצליחה. אפשר להכניס את הערך למשתנה ולהשתמש בו.

ניווט במאמר

מאמרים אחרונים

Weekly Tutorial