1 / 21

CS360

CS360. Windows Programming. Last Week. We Covered the principles behind web services Introduced the .NET framework Reviewed object-oriented programming Started learning about C#. C# Statements and Expressions. Very similar to C++, with some changes to increase robustness

jiro
Download Presentation

CS360

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. CS360 Windows Programming CS360 Windows Programming

  2. Last Week • We • Covered the principles behind web services • Introduced the .NET framework • Reviewed object-oriented programming • Started learning about C# CS360 Windows Programming

  3. C# Statements and Expressions • Very similar to C++, with some changes to increase robustness • No ‘->’ or ‘::’; all qualifications use ‘.’ • Local variables must be initialized before use • if, while, do require bool condition • goto can’t jump into blocks • switch statement – no fall through (empty case OK) • case labels can be strings CS360 Windows Programming

  4. Types • FCL is a library of types • Classes – today • Structs – today • Interfaces – next time • Enumerations – next time • Delegates – next time CS360 Windows Programming

  5. Classes • Classes contain • Fields - data members in C++ • Methods - member functions in C++ • Properties - expose data using get and set • Events - define notifications that class can hire CS360 Windows Programming

  6. Example class Rectangle { // Fields protected int width = 1; protected int height = 1; // Methods (constructors) public Rectangle () {} public Rectangle (int cx, int cy) { Width = cx; Height = cy; } } CS360 Windows Programming

  7. Example (cont.) // Properties public int Width { get { return width; } set { if (value > 0) width = value; else throw new ArgumentOutOfRangeException ( "Width must be 1 or higher"); } } CS360 Windows Programming

  8. Example (cont.) public int Height { get { return height; } set { if (value > 0) height = value; else throw new ArgumentOutOfRangeException ( "Height must be 1 or higher"); } } public int Area { get { return width * height; } } CS360 Windows Programming

  9. Example (cont.) Rectangle rect = new Rectangle (); Rectangle rect = new Rectangle (3, 4); rect.Width *= 2; int area = rect.Area; CS360 Windows Programming

  10. Structs struct Point { public int x; public int y; public Point (int x, int y) { this.x = x; this.y = y; } } CS360 Windows Programming

  11. Reference and Value Types • Classes are reference types • Structs are value types CS360 Windows Programming

  12. Common Type System Object Value Reference Built-in (primitive) data types Examples: int, double, … Size is specific Allocated on Stack Assignment copies value Cannot derive from other types User defined types: structs Deallocated when defining block exits Examples: Classes, Arrays, Interfaces, Delegates Allocated on managed heap Assignment copies reference Garbage collected CS360 Windows Programming

  13. Stack and Heap • Memory is a collection of bytes • Imagine each box as one byte long • Each byte can hold a number between 0-255. Why? • Each byte has an address CS360 Windows Programming

  14. Stack and Heap • Whenever a variable is created it is added on to the stack • Variables are added sequentially (i.e. l-r, t-b) • No value for an empty byte • No holes allowed in memory. Why? • Stack pointer pointsto end of usedmemory A B C CS360 Windows Programming

  15. Stack and Heap • Problem: what do we do with dynamic variables? • Solution: use a heap • Stack is used forfixed size variables only • Stack pointer infirst four bytes ofstack Stack pointer A B B B C CS360 Windows Programming

  16. Stack and Heap • Variables created on the heap are scattered throughout the heap • Heap contains holes • Each variable has a header listing • Size of variable • Address of nextvariable stack heap CS360 Windows Programming

  17. Example Heap Stack Static void Main() { Ref r1 = new Ref(); Val v1 = new Val(); } Class Ref { public int x; } struct Val { public int x; } CS360 Windows Programming

  18. Example Heap Stack Static void Main() { Ref r1 = new Ref(); Val v1 = new Val(); } Class Ref { public int x; } struct Val { public int x; } r1 x = 0 v1 x = 0 CS360 Windows Programming

  19. Example Heap Stack Static void Main() { Ref r1 = new Ref(); Val v1 = new Val(); r1.x = 5; v1.x = 5; } Class Ref { public int x; } struct Val { public int x; } r1 x = 5 v1 x = 5 CS360 Windows Programming

  20. Example Heap Stack Static void Main() { Ref r1 = new Ref(); Val v1 = new Val(); r1.x = 5; v1.x = 5; Ref r2 = r1; Val v2 = v1; } Class Ref { public int x; } struct Val { public int x; } r1 x = 5 v1 x = 5 r2 v2 x = 5 CS360 Windows Programming

  21. Summary • Completed p. 27 - 38 • Next time we will complete chapter 2 CS360 Windows Programming

More Related