Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview of C#. Agenda Structure of a C# Program Basic Input/Output Operations Compiling, Running, and Debugging Namespaces.

Similar presentations


Presentation on theme: "Overview of C#. Agenda Structure of a C# Program Basic Input/Output Operations Compiling, Running, and Debugging Namespaces."— Presentation transcript:

1 Overview of C#

2 Agenda Structure of a C# Program Basic Input/Output Operations Compiling, Running, and Debugging Namespaces

3 Agenda Structure of a C# Program Basic Input/Output Operations Compiling, Running, and Debugging Namespaces

4 Structure of a C# Program using System; class HelloWorld { public static void Main() { Console.WriteLine("Hello C# !!!"); }

5 Structure of a C# Program בשפת C# כל הקוד של התוכנית נכתב רק בקבצים בעלי הסיומת CS. בדומה ל-C/C++, C# מבחינה בין אותיות קטנות לאותיות ראשיות (Case Sensitive), ולכן חשוב להקפיד על Upper Case ועל Lower Case. C# הנה שפה מונחית אובייקטים, ניתן אף להגדיר את C# כ – Pure Object Oriented Language.

6 Structure of a C# Program המושג גלובאלי נעלם, כל משתנה/פונקציה שייכים למחלקה מסויימת בתוכנית. using System; class HelloWorld { public static void Main() { Console.WriteLine("Hello C# !!!"); }

7 Structure of a C# Program הפונקציה הראשית של התוכנית, נקודת הכניסה לתוכנית. המתודה Main חייבת להיות מתודה static. היא יכולה להחזיר ערך או להחזיר void ויכולה לקבל פרמטרים אם יש בכך צורך. כל תוכנית חייבת להכיל לפחות מתודת Main אחת. using System; class HelloWorld { public static void Main() { Console.WriteLine("Hello C# !!!"); }

8 Structure of a C# Program Console הנה מחלקה המציעה אוסף של מתודות המטפלות בקלט/פלט. מוגדרת במרחב השמות System. using System; class HelloWorld { public static void Main() { Console.WriteLine("Hello C# !!!"); }

9 Agenda Structure of a C# Program Basic Input/Output Operations Recommended Practices Compiling, Running, and Debugging Namespaces

10 Basic Input/Output Operations שליחת מידע לאמצעי הפלט הסטנדרטי : Console.Write(…) Console.WriteLine(…) קבלת מידע מאמצעי הפלט הסטנדרטי Console.Read(…) Console.ReadLine(…)

11 Basic Input/Output Operations המתודה Console.Write(…) using System; class ConsoleApp { static void Main(string[] args) { Console.Write("num"); Console.Write(" = "); Console.Write(13); Console.Write(“num = 13”) } שליחת מידע לחלון ללא ירידת שורה. המתודה מועמסת.

12 Basic Input/Output Operations המתודה Console.WriteLine(…) using System; class ConsoleApp { static void Main(string[] args) { Console.WriteLine(100); Console.WriteLine("Hello World!!!"); Console.WriteLine(100 + " littles dwarfs launched to the moon"); Console.WriteLine(); } שליחת מידע לחלון ללא ירידת שורה. המתודה מועמסת.

13 Basic Input/Output Operations Text Formatting המתודות Write ו- WriteLine מאפשרות לפרמט את הפלט על ידי שימוש בסמנים (Markers). לדוגמה: Console.WriteLine("The Sum of {0} and {1} is {2}.", 12,23,12+23); הפלט: The Sum of 12 and 23 is 35 חייבים לשמור על מיספור עוקב של הסמנים, אולם הם אינם חייבים להופיע על פי הסדר, כל סמן יכול להופיע יותר מפעם אחת במחרוזת הפלט.

14 Basic Input/Output Operations Numeric Formatting המתודות Write ו-WriteLine מאפשרות לעצב את הערכים המספריים. לדוגמה: המתג C מציין הדפסת מטבע Console.WriteLine("The Sum of {0:C} and {1:C} is {2:C}.", 12,23,12+23); הפלט: The Sum of $12 and $23 is $35

15 Basic Input/Output Operations Numeric Formatting- דוגמאות נוספות: המתג F קובע הדפסת מספר ממשי. המספר הבא אחריו קובע את מספר הספרות אחרי הנקודה שיוצגו Console.WriteLine("Salary = {0:F2}",9999.12345); הפלט: Salary = 9999.12. המתג N מגדיר הדפסת מספר ארוך עם פסיקים. Console.WriteLine("Num = {0:N} ", 1000000); הפלט: 1,000,000 הדפסת מספר כ – exponential. Console.WriteLine("The Sum of {0:E} and {1:E} is {2:E}.",12,23,12+23); הפלט: The Sum of 1.200000E+001 and 2.300000E+001 is 3.500000E+001. הדפסת מספר כ- hexadecimal Console.WriteLine("The Sum of {0:X} and {1:X} is {2:X}.",12,98,12+98); הפלט The Sum of C and 62 is 6E

16 Basic Input/Output Operations המתודה Console.Read(…) using System; class ConsoleApp { static void Main(string[] args) {... int num = 0; num = Console.Read(); Console.WriteLine("num = {0}",num);... } המתודה Read קוראת תו מהמקלדת. הערך המוחזר הנו ערכו של התו בטבלת Unicode. לדוגמה: במידה ונקליד את התו '1', ערכו של num יהיה 49

17 Basic Input/Output Operations המתודה Console.ReadLine(…) class ConsoleApp { static void Main(string[] args) {... string name; name = Console.ReadLine(); Console.WriteLine(name); } המתודה Console.ReadLine(...) קוראת תווים מהמקלדת עד ל- Enter ומחזירה מחרוזת

18 Agenda Structure of a C# Program Basic Input/Output Operations Compiling, Running, and Debugging Namespaces

19 Compiling, Running, and Debugging הקומפיילר של שפת C# נקרא CSC.EXE. הקומפיילר מותקן כחלק מההתקנה של.NET Framework. המיקום הפיזי של תוכנת הקומפיילר הוא ב- C:\WINNT\Microsoft.NET\Framework\v1.0.3705 (מספר הגירסה יכול כמובן להשתנות). התוצר של הקומפיילר הוא קובץ EXE או DLL. התוצר אינו Native Code (שפת מכונה) אלא MSIL (Microsoft Intermediate Language). קיבצי ה- EXE או ה- DLL המקומפל ל- Native Code מכונים Assemblies.

20 Compiling, Running, and Debugging קומפילציה פשוטה: csc.exe c:\Programs\FirstApp.cs התוצר: FirstApp.exe קביעת שם קובץ ההרצה: csc.exe /out:App.exe c:\Programs\FirstApp.cs התוצר: App.exe קומפילציה לפרוייקט המכיל מספר קבצים: csc.exe /out:App.exe MyFile1.cs MyFile2.cs קומפילציה למספר קבצים רב: csc.exe /out:App.exe *.cs

21 Compiling, Running, and Debugging קומפילציה למספר קבצים תוך ציון המחלקה בה מוגדרת Main : csc.exe / out:App.exe /main:MyClass MyFile1.cs MyFile2.cs קומפילציה עם אופטימיזציה: csc.exe c:\Programs\FirstApp.cs /Optimize הוספת ICON לקובץ ההרצה: csc.exe /Win32icon:MyIcon.ico FirstApp.cs

22 Compiling, Running, and Debugging התוצר של הקומפילציה הוא קובץ EXE (או DLL). קובץ ההרצה אינו Native Code אלא MSIL Microsoft Intermediate Language) ) בזמן ריצה מומר ה- MSIL ל- Native Code על ידי ה- JIT.

23 Compiling, Running, and Debugging כלים: 1 – NGEN.EXE – מייצר Image מקובץ ה- MSIL, ה- Image נשמר כ- Native Code ובכך משפר את הביצועים. 2 – ILDASM.EXE – מאפשר לצפות בתוכן ה- Assemblies.

24 Agenda Structure of a C# Program Basic Input/Output Operations Compiling, Running, and Debugging Namespaces

25 יישות לוגית הגדרתית. מרכז אוסף של הגדרות טיפוסים (Classes, Structures, Enumerations ) בעלי קשר לוגי ביניהם. מרחב שמות אחד יכול להיות משותף למספר קבצים או פרוייקטים. מונע התנגשות שמות (Name Clashes) יכול להיות מקונן (Nested) namespace Shapes { class Point {... } class Circle {... }

26 Namespaces לפני השימוש במחלקה יש להצהיר על מרחב השמות בו היא מוגדרת. ההגדרה יכולה להיות מפורשת או מרומזת. הגדרה מפורשת : MyCompany.Shapes.Point p = new MyCompany.Shapes.Point(); הגדרה מרומזת: מחייבת את השימוש במילה השמורה using. using MyCompany.Shapes; … Point p = new Point();

27 Namespaces כינויים : ניתן להגדיר כינויים למרחבי שמות או מחלקות. using Point1 = Microsoft.Shapes.Point; using Point2 = IBM.Shapes.Point; namespace NamespaceSample { class App { static void Main(string[] args) { Point1 p1 = new Point1(); Point2 p2 = new Point2(); }

28 Review Structure of a C# Program Basic Input/Output Operations Compiling, Running, and Debugging Namespaces

29 www.corner.co.il Books Corner – הוצאת ספרים מקוונת


Download ppt "Overview of C#. Agenda Structure of a C# Program Basic Input/Output Operations Compiling, Running, and Debugging Namespaces."

Similar presentations


Ads by Google