1 / 15

C#: Udtryk og metoder

C#: Udtryk og metoder. Indhold. “With regards to programming statements and methods, C# offers what you would come to expect from a modern OOPL…” Udtryk Metoder. Part 1. Udtryk…. Udtryk i C#. C# har standard udtrykene … Værditildeling Subrutiner og funktionskald Betingelser

lucus
Télécharger la présentation

C#: Udtryk og metoder

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C#: Udtryk og metoder

  2. Indhold “With regards to programming statements and methods, C# offers what you would come to expect from a modern OOPL…” • Udtryk • Metoder

  3. Part 1 Udtryk…

  4. Udtryk i C# C# har standard udtrykene… • Værditildeling • Subrutinerogfunktionskald • Betingelser • if, switch • Iteration • for, while, do-while • Control Flow • return, break, continue, goto

  5. Eksempler x = obj.foo(); if (x > 0 && x < 10) count++; else if (x == -1) ... else { ... } while (x > 0) { ... x--; } for (int k = 0; k < 10; k++) { ... }

  6. Andre udtryk • C# har også… • iteration gennem en datastruktur via foreach • namespace importering via using

  7. foreach • Specialiceretforeachløkketil sweep gennem f.eks array • reducererrisiko for indekseringsfejl • Giver read only tilgang int[] data = { 1, 2, 3, 4, 5 }; int sum = 0; foreach (intxin data) { sum += x; } foreach type value collection

  8. using • using direktiv giver adgangtilklasseri et namespace uden at skulleangivedethver gang namespace Workshop { public class Customer { . . . } public class Product { . . . } } // before Workshop.Customer c; c = new Workshop.Customer("joe hummel", 94652); //after using Workshop; Customer c; c = new Customer("joe hummel", 94652);

  9. Et eksempel namespace Workshop { public class Customer { . . . } public class Product { . . . } } • using direktiv(er) angivesitoppenaffilen /* main.cs */ using System; using Workshop; public class App { public static void Main() { Customer c; c = new Customer("joe hummel", 94652); Console.WriteLine( c.ToString() ); } }

  10. Part 2 Metoder…

  11. Typer af methoder • Klasser kan indeholde 2 typer af methoder: • instance • static • Instance methoder forudsætter instancering af et objekt • Static methoder er globale og kræver kun klassenavnet

  12. Eksempel • Array klasseni FCL • fully-qualified name is System.Array namespace System { public class Array { public int GetLength(int dimension) { ... } public static void Sort(Array a) { ... } . . . } } instance metode(static ikke angivet) static metode(static angivet)

  13. Metodekald • Metodekald i Array klassen: /* main.cs */ using System; public class App { public static void Main() { int[] data = { 11, 7, 38, 55, 3 }; Array.Sort(data); for (int i=0; i<data.GetLength(0); i++) Console.WriteLine(i + ": " + data[i]); } }

  14. Andre nyttige static metoder • Et program til addere 2 heltal og udskrive summen: using System; public class Calculator { public static void Main() { string input, output; int a, b, sum; Console.Write("Enter first integer: "); input = Console.ReadLine(); a = Convert.ToInt32(input); Console.Write("Enter second integer: "); input = Console.ReadLine(); b = Convert.ToInt32(input); sum = a + b; output = String.Format("{0} + {1} = {2}", a, b, sum); Console.WriteLine(output); } }

  15. Opsummering • Standardudtryk, og et par ikke-standard • assignment, if, for, while, foreach, using • To typer of metoder • instance metoder kræver et objekt • static metoder er globale og kræver kun en klasse

More Related