1. נניח שנרצה להפוך את car **לאוסף של מחרוזות**
static void Main(string[] args)
{
string car = "BMW";
Console.WriteLine(car);
}
2. הוספנו: סוגריים **מסולסלים**, לשון **רבים**, וסוגריים **מרובעים**,
static void Main(string[] args)
{
string[] cars = { "BMW", "Ford", "Kia" };
Console.WriteLine(cars); // ⟹😟לא כמו פייתון. לא ממש עוזר System.String[] מדפיס
}
3. ניתן לגשת לאיבר במערך **לפי מיקום**
static void Main(string[] args)
{
string[] cars = { "BMW", "Ford", "Kia" };
Console.WriteLine(cars[0]); // prints BMW, מתנהג כמו במחרוזת
Console.WriteLine(cars[0][1]); // ??? ומה זה ידפיס
}
4. כאן כבר יש לנו **בעיה**
static void Main(string[] args)
{
string[] cars = { "BMW", "Ford", "Kia" };
Console.WriteLine(cars[0]);
Console.WriteLine(cars[1]);
Console.WriteLine(cars[2]);
Console.WriteLine(cars[3]); //index out of range exception
// Program WILL CRUSH
Console.ReadLine();
}
5. אפשר לטפל במצבי **Exception**
static void Main(string[] args)
{
string[] cars = { "BMW", "Ford", "Kia" };
try
{
Console.WriteLine(cars[0]);
Console.WriteLine(cars[1]);
Console.WriteLine(cars[2]);
Console.WriteLine(cars[3]); // 😥Index Out of Range exception😥
}
catch (Exception e)
{
Console.WriteLine($"we had aproblem: {e.Message}");
}
}
6. ניעזר בלולאות כדי **לעבור על כל איברי המערך**, אבל,
static void Main(string[] args)
{
string[] cars = [ "BMW", "Ford", "Kia" ]; // 🤔 ??? {מסולסלים} לא היו קודם סוגריים 😲
for (int i = 0; i < cars.Length; i++)
Console.WriteLine(cars[i]); // 👮 i גישה ישירה לאיבר באינדקס
}
7. **foreach יותר נוח** בהרבה מקרים
static void Main(string[] args)
{
string[] cars = { "BMW", "Ford", "Kia" }; //inline initialization
foreach (string car in cars) 🐭
Console.WriteLine(car); // הרבה יותר פשוט
}
8. כאן מקצים מערך בגודל מסויים וזה **סוף פסוק**
static void Main(string[] args)
{
string[] cars = new string[5]; // איתחול לגודל 5. לא יורשה לשנות את הגודל בהמשך
// ולכן בהמשך הדרך בפרוייקטים נעבוד עם מבנים אחרים
// אסור בשימוש Array.Resize(ref cars, 10); אסור בשימוש
for (int i = 0; i < cars.Length; i++)
{
cars[i] = "BMW" + i;
Console.WriteLine(cars[i]);
}
}
9. הבדל **חשוב** בין סוגי הלולאות - **לא ניתן לבצע השמה ב-foreach**
static void Main(string[] args)
{
string[] cars = new string[5]; // מקצה מערך בגודל 5
foreach (string car in cars)
car = "BMW"; // ===== !!! השמה - לא אפשרית ======
// ועדיין, נח ושימושי כשעובדים עם עצמים
}
10. אפשר **לשלוח מערך כארגומנט** לפונקציה.
static void Main(string[] args)
{
int[] nums = { 3,2,1 };
Add10(nums);
PrintArr(nums); // prints 13 12 11
PrintArr(["bus", "bug", "beer", "bear"]); //instanciation in a call with [ ] ???
}
public static void Add10(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
arr[i] += 10;
}
static void PrintArr<T>(T[] arr) // מה נסגר עם הפונקציות הגנריות האלה?
{
foreach (var item in arr)
Console.Write($" {item} ");
Console.WriteLine();
}
11. המערך **מאותחל ל-nulls** או לאפסים או ל-false בהתאם לטיפוס
static void Main(string[] args)
{
string[] cars = new string[5]; // אז לא יודפס כלום null אפשר להדפיס את איברי המערך. הם כולם
//=== = "" קיימת דרישה בבחינות לבצע לולאת איתחול שמאפסת את אברי המערך. או במקרה זה מגדירה את כולם ===
//=== מאד לא סביר ומתנגש עם העבודה עם עצמים בהמשך === אמשיך לברר לכם את הנקודה
Console.Write(cars[0].Length); // Null Reference Exception אבל לא ניתן לגשת לתכונה כשאין עדין עצם
cars[0] = cars[0] + "wow"; // null ובכל זאת ניתן לשרשר מחרוזת עם
}
12. בואו ננסה להבין, **מה זה object reference**
static void Main(string[] args)
{
char[] chars = ['h', 'e', 'l', 'l', 'o'];
PrintArr(chars); // h e l l o
WillItChange_יתשנה_או_לא(chars);
PrintArr(chars); // Stays h e l l o
}
static void WillItChange_יתשנה_או_לא(char[] arr)
{
arr = ['h', 'e', 'l', 'l', '_', 'N', 'o'];
}
13. דוגמא עם ternary operator
static void Main(string[] args)
{
string[] cars = ["BMW", "Ford", "Kia" ,"T"]; // 🤔 ??? {מסולסלים} לא היו קודם סוגריים 😲
for (int i = 0; i < cars.Length; i++)
{
//ternary operator
Console.WriteLine($"{cars[i]} is " +
$"{cars[i].Length} meter{(cars[i].Length>1 ? "s" : "")} long"); // 👮 i גישה ישירה לאיבר באינדקס
}
}
14. דוגמא עם ternary operator with higher perf
static void Main(string[] args)
{
string[] cars = ["BMW", "Ford", "Kia" ,"T"]; // 🤔 ??? {מסולסלים} לא היו קודם סוגריים 😲
for (int i = 0; i < cars.Length; i++)
{
//ternary operator
string c = cars[i]; // 👮 i גישה ישירה לאיבר באינדקס
string sOrNos = c.Length > 1 ? "s" : ""; // Ternary
Console.WriteLine($"{c} is {c.Length} meter{sOrNos} long"); // 👮 i גישה ישירה לאיבר באינדקס
}
}
15. דוגמא עם ternary operator in a foreach
static void Main(string[] args)
{
string[] cars = ["BMW", "Ford", "Kia" ,"T"];
foreach (var c in cars)
{
string sOrNos = c.Length > 1 ? "s" : ""; // Ternary
Console.WriteLine($"{c} is {c.Length} meter{sOrNos} long");
}
}
המשך למידה
⬅ עברו לפרק 9a - גרסת ללא אנימציות
⬅ עברו לפרק 9b - הערות והרחבות
⬅ עברו לפרק 9c - מערך מונים וצוברים
תרגול
⬅ עברו לרשימת שקפי תרגול במערכים במצגת קמפוס
⬅ עברו לתרגול 9.1 - מערך חד ממדי
⬅ עברו לתרגול 9.2 - מערכים - שאלות ב- CodeWars