Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 09 Dr. Eng. Ibrahim El-Nahry C# - Structures, Enumerations and Partial Classes."— Presentation transcript:

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

2 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

3 However, you cannot define a default const- ructor (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

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

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

6 6

7 7

8 Unified Type System Everything is an object All types ultimately inherit from object Any piece of data can be stored, transported, and manipulated with no extra work Stream MemoryStreamFileStream Hashtabledoubleint object

9 All Types Descend From Object All value and reference types are derived (indirectly or directly) from the object class object has 4 methods methods object.Equals(object other) object.GetType() object.ToString() object.GetHashCode() 9

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

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

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

13 C# Memory Management Allocation using new Deallocation by Garbage Collection Garbage collection: 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

14 Classes vs. Structs 14

15 Example 1 Defining a struct 15

16 Instantiating the struct 16

17 Instantiating using new and without using new 17 Output: book1: Mastering C# in 3 Days Book2: The History of Telephone

18 18 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 p1, int p2) { x = p1; y = p2; } class MainClass { public static void Main() { // Initialize: Point myPoint = new Point(); Point yourPoint = new Point(10,10); // Display results: Console.Write("My Point: "); Console.WriteLine("x = {0}, y = {1}", myPoint.x, myPoint.y); Console.Write("Your Point: "); Console.WriteLine("x = {0}, y = {1}", yourPoint.x, yourPoint.y); } Output My Point: x = 0, y = 0 Your Point: x = 10, y = 10

19 19 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 MainClass { public static void Main() { Point myPoint; myPoint.x = 10; myPoint.y = 20; Console.WriteLine("My Point:"); Console.WriteLine("x = {0}, y = {1}", myPoint.x, myPoint.y); } Output My Point: x = 10, y = 20

20 20 Example 4

21 21

22 22

23 Enumerations 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

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

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

28 28 using System; class Program { enum E { None, BoldTag, ItalicsTag, HyperlinkTag } static void Main() { E en1 = E.BoldTag; E en2 = E.ItalicsTag; if (en1 == E.BoldTag) { Console.WriteLine("Bold"); } if (en1 == E.HyperlinkTag) { Console.WriteLine("Not true"); } Output Bold Example 3

29 29 using System; public class EnumTest { 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.WriteLine("Sun = {0}", x); Console.WriteLine("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 Example 4

30 30 Example 5

31 31

32 Partial Classes 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

33 Partial Types New keyword partial Separate the definition of a class, a struct, an interface over two or more source files (but not enumeration) //first file (MyClass_1.cs) public partial class MyClass { private int nCount;..... } //second file (MyClass_2.cs) public partial class MyClass { private bool isPresent..... } 33

34 Partial Types 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

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


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

Similar presentations


Ads by Google