Menu
CoddyTech

מרחבי שמות וקבצים

שיעור 29 מתוך 48 בקורס C# למפתחי GDScript של Coddy.

‏GDScript מתייחס לכל קובץ .gd כמחלקה, ו־class_name רושם אותה במרחב שמות גלובלי אחד; preload טוען סקריפט לפי נתיב. כל קובצי C# מקומפלים יחד, מרחבי שמות מקבצים שמות של טיפוסים, ו־using משפיע רק על חיפוש שמות. כמו ב־GDScript, הקוד רץ רק כשקוראים למתודה, לעולם לא ברמת הקובץ.

המהדר משלב את כל קובצי המקור לתוכנית אחת. שמות מרחבי שמות מארגנים טיפוסים, ו־using מכניס שמות לתחום; הוא לא טוען קובץ כפי ש־preload עושה. public קובע את הגישה בין הטיפוסים האלה. נדרש Main אחד בדיוק, גם כשיש מחלקות רבות.

הפניה ל־GDScript

# pricing.gd
class_name Pricing

static func total(unit: int, count: int) -> int:
	return unit * count

# main.gd
extends Node

func _ready():
	print(Pricing.total(2, 5))

הצהרות תומכות ב־C#

namespace Display { public static class Text { public static string Bracket(string value) { return "[" + value + "]"; } } }

פקודות C# בתוך Main

Console.WriteLine(Display.Text.Bracket("pen"));

אלה קטעים ממוקדים לצורכי הוראה. קובצי האתגר כוללים מעטפות הרצה מלאות; הקטעים שלמעלה מציגים את ההצהרות והפקודות הרלוונטיות.

פלט לדוגמה:

10

פלט C#:

[pen]
challenge icon

אתגר

קל

ממשו את Catalog.Pricing.Total בקובץ Pricing.cs. הפונקציה מחזירה את unit * quantity עבור קלטים שלמים לא־שליליים. Solve קוראת לפונקציית העזר עם מרחב השמות. תוכנית הנהג קוראת לה גם ישירות.

ערכו את קובצי C# שאינם נעולים. הקובץ הגלוי והנעול Main.cs קורא את השורות המתוארות, קורא ל־Exercise.Solve, מוודא שהקלט לא השתנה ומדפיס את התוצאה עם תו ירידת שורה אחד בסוף. השאירו את ממשקי ה־API המוצהרים. אל תוסיפו Main נוסף ואל תדפיסו פלט אבחון.

המפעיל הנעול בודק גם בנפרד את ה־API של פונקציית העזר או המחלקה המיוצאת, ולא רק את העוטפת Solve.

נסו בעצמכם

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
public class Program {
static int I(string text) { return int.Parse(text,CultureInfo.InvariantCulture); }
static int[] Ints(string text) { return text.Length==0 ? new int[0] : text.Split(',').Select(I).ToArray(); }
static string[] Words(string text) { return text.Length==0 ? new string[0] : text.Split(','); }
static string Snapshot(object value) { if (value==null) return "<null>"; if (value is string) return (string)value; var sequence=value as System.Collections.IEnumerable; if (sequence!=null) { var items=new List<string>(); foreach (object item in sequence) items.Add(Snapshot(item)); return string.Join("|",items); } return Convert.ToString(value,CultureInfo.InvariantCulture); }
public static void Main() {
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var rows = new List<string>(); string line; while ((line=Console.ReadLine()) != null) rows.Add(line);
string[] input=rows.ToArray();
var a0 = I(input[0]); string before0=Snapshot(a0);
var a1 = I(input[1]); string before1=Snapshot(a1);
if (Catalog.Pricing.Total(7,3) != 21 || Catalog.Pricing.Total(4,0) != 0) throw new Exception("Pricing API");
var result = Exercise.Solve(a0,a1);
if (Snapshot(a0) != before0) throw new Exception("Input mutation");
if (Snapshot(a1) != before1) throw new Exception("Input mutation");
Console.WriteLine(result);
}
}
quiz iconבחנו את עצמכם

השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.

כל השיעורים ביחידה C# למפתחי GDScript

תרגלו בעצמכם: קומפיילר C# אונליין