Lecture 09 C Structures Enumerations and Partial Classes

  • Slides: 35
Download presentation
 Lecture 09 C# - Structures, Enumerations and Partial Classes Dr. Eng. Ibrahim El-Nahry

Lecture 09 C# - Structures, Enumerations and Partial Classes Dr. Eng. Ibrahim El-Nahry

 Structures A structure is similar to a class, but it is of value

Structures A structure is similar to a class, but it is of value type. cannot inherit or be a base for other struc tures or class (but it inherit object) can implement one or more interface can define constructors, but not destructors 2

 However, you cannot define a default constructor (no parameters) can be created using

However, you cannot define a default constructor (no parameters) can be created using new or performed the in itialization manually. a struct was accessed directly, not through reference variable, so it saved space and got more efficiency. 3

Type System n Value types n n n Directly contain data Cannot be null

Type System n Value types n n n Directly contain data Cannot be null Reference types n n Contain references to objects May be null int i = 123; string s = "Hello world"; i s 123 "Hello world"

Type System n Value types n n Primitives Enums Structs int i; enum State

Type System n Value types n n Primitives Enums Structs int i; enum State { Off, On } struct Point { int x, y; } Reference types n n Classes Interfaces Arrays Delegates class Foo: Bar, IFoo {. . . } interface IFoo: IBar {. . . } string[] a = new string[10]; delegate void Empty();

6

6

7

7

Unified Type System n Everything is an object n n All types ultimately inherit

Unified Type System n Everything is an object n n All types ultimately inherit from object Any piece of data can be stored, transported, and manipulated with no extra work object Stream Memory. Stream Hashtable File. Stream int double

All Types Descend From Object All value and reference types are derived (indirectly or

All Types Descend From Object All value and reference types are derived (indirectly or directly) from the object class n object has 4 methods �� object. Equals(object other) �� object. Get. Type() �� object. To. String() �� object. Get. Hash. Code() n 9

Garbage collection The “new” operator used to create an object n Objects stored on

Garbage collection The “new” operator used to create an object n Objects stored on heap and when there are no more live references to them they are discarded by the automatic garbage collector n The. NET framework takes care of getting rid of your objects n It runs automatically n You can run it manually by calling GC. Collect() n 10

Memory Layout Heap Stack f calls g, g calls h, h calls k f()

Memory Layout Heap Stack f calls g, g calls h, h calls k f() g() h() k() Method Call & Return P* Reference = pointer Garbage Collector 11

C# Memory Management n n n Static vs. dynamic Dynamic storage—stack and heap Stack

C# Memory Management n n n Static vs. dynamic Dynamic storage—stack and heap Stack (Dynamic): n n Managed algorithmically by implementation of method calls Heap (Dynamic) n n Mostly managed by system Provision for management by programmer 12

C# Memory Management n n n Allocation using new Deallocation by Garbage Collection Garbage

C# Memory Management n n n Allocation using new Deallocation by Garbage Collection Garbage collection: n n Tracks objects that are accessible Frees storage associated with objects that are inaccessible Garbage collector is a system provided service that runs periodically Deals with fragmentation 13

Classes vs. Structs 14

Classes vs. Structs 14

Example 1 Defining a struct 15

Example 1 Defining a struct 15

Instantiating the struct 16

Instantiating the struct 16

Instantiating using new and without using new Output: book 1: Mastering C# in 3

Instantiating using new and without using new Output: book 1: Mastering C# in 3 Days Book 2: The History of Telephone 17

Example 2 This example demonstrates struct initialization using both default and parameterized constructors. using

Example 2 This example demonstrates struct initialization using both default and parameterized constructors. using System; public struct Point { public int x, y; public Point(int p 1, int p 2) { x = p 1; y = p 2; } } class Main. Class { public static void Main() { // Initialize: Point my. Point = new Point(); Point your. Point = new Point(10, 10); // Display results: Console. Write("My Point: "); Console. Write. Line("x = {0}, y = {1}", my. Point. x, my. Point. y); Console. Write("Your Point: "); Console. Write. Line("x = {0}, y = {1}", your. Point. x, your. Point. y); } } Output My Point: x = 0, y = 0 Your Point: x = 10, y = 10 18

Example 3 This example demonstrates a feature that is unique to structs. It creates

Example 3 This example demonstrates a feature that is unique to structs. It creates a Point object without using the new operator. If you replace the word struct with the word class, the program won't compile. using System; public struct Point { public int x, y; public Point(int x, int y) { this. x = x; this. y = y; } } class Main. Class { public static void Main() { Point my. Point; my. Point. x = 10; my. Point. y = 20; Console. Write. Line("My Point: "); Console. Write. Line("x = {0}, y = {1}", my. Point. x, my. Point. y); } } Output My Point: x = 10, y = 20 19

Example 4 20

Example 4 20

21

21

22

22

Enumerations n n n Enumerations are special sets of named values which all maps

Enumerations n n n Enumerations are special sets of named values which all maps to a set of numbers, usually integers. Enumerations store special values. They make programs simpler. If you place constants directly where used, your C# program becomes complex. It becomes hard to change. Enumerations instead keep these magic constants in a distinct type 23

24

24

25

25

Example 1 using System; class Program { enum Importance { None, Trivial, Regular, Important,

Example 1 using System; class Program { enum Importance { None, Trivial, Regular, Important, Critical } static void Main() { Importance value = Importance. Critical; if (value == Importance. Trivial) { Console. Write. Line("Not true"); } else if (value == Importance. Critical) { Console. Write. Line("True"); } } } Output True 26

Example 2 using system; enum Color { Red, Green, Blue } class Test {

Example 2 using system; enum Color { Red, Green, Blue } class Test { static void Print. Color(Color color) { switch (color) { case Color. Red: Console. Write. Line("Red"); break; case Color. Green: Console. Write. Line("Green"); break; case Color. Blue: Console. Write. Line("Blue"); break; default: Console. Write. Line("Unknown color"); break; } } static void Main() { Color c = Color. Red; Print. Color(c); Print. Color(Color. Blue); } } Output Red Blue 27

Example 3 using System; class Program { enum E { None, Bold. Tag, Italics.

Example 3 using System; class Program { enum E { None, Bold. Tag, Italics. Tag, Hyperlink. Tag } static void Main() { E en 1 = E. Bold. Tag; E en 2 = E. Italics. Tag; if (en 1 == E. Bold. Tag) { Console. Write. Line("Bold"); } if (en 1 == E. Hyperlink. Tag) { Console. Write. Line("Not true"); } } } Output Bold 28

Example 4 using System; public class Enum. Test { enum Days { Sat=1, Sun,

Example 4 using System; public class Enum. Test { enum Days { Sat=1, Sun, Mon, Tue, Wed, Thu, Fri } public static void Main() { int x = (int) Days. Sun; int y = (int) Days. Fri; Console. Write. Line("Sun = {0}", x); Console. Write. Line("Fri = {0}", y); } } Output Sun = 2 Fri = 7 Notice that if you remove the initializer from Sat=1, the result will be: Sun = 1 Fri = 6 29

Example 5 30

Example 5 30

31

31

Partial Classes n n n Partial classes span multiple files. With partial, you can

Partial Classes n n n Partial classes span multiple files. With partial, you can physically separate a class into multiple files. This is often done by code generators. With normal C# classes, you cannot declare a class in two separate files in the same project. But with the partial modifier, you can. This is useful if one file is commonly edited and the other is machine-generated or rarely edited. 32

Partial Types n n New keyword partial Separate the definition of a class, a

Partial Types n n New keyword partial Separate the definition of a class, a struct, an interface over two or more source files (but not enumeration) //first file (My. Class_1. cs) public partial class My. Class { private int n. Count; . . . } //second file (My. Class_2. cs) public partial class My. Class { private bool is. Present. . . } 33

Partial Types n n The most common use of this type in GUI designer.

Partial Types n n The most common use of this type in GUI designer. Another use case is where two programmers want to work on different methods of the same class at the same time 34

class Program { static void Main() { A. A 1(); A. A 2(); }

class Program { static void Main() { A. A 1(); A. A 2(); } } Example Contents of file A 1. cs [C#] using System; partial class A { public static void A 1() { Console. Write. Line("A 1"); } } Contents of file A 2. cs [C#] using System; partial class A { public static void A 2() { Console. Write. Line("A 2"); } } Output A 1 A 2 35