1 / 30

Fun with C# Episode XIV: Value Types Strike Back

Learn about hiding members in C# subclass and the behavior of value types in different scenarios.

rcosta
Download Presentation

Fun with C# Episode XIV: Value Types Strike Back

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. FUN WITH C# EPISODE XIV VALUE TYPES STRIKE BACK

  2. Hiding Members can be declared as new in a subclass. They hide inherited members with the same name and signature. class A { public int x; public void F() {...} public virtual void G() {...} } class B : A { public newint x; public new void F() {...} public new void G() {...} } B b = new B(); b.x = ...; // accesses B.x b.F(); ... b.G(); // calls B.F and B.G ((A)b).x = ...; // accesses A.x! ((A)b).F(); ... ((A)b).G(); // calls A.F and A.G!

  3. CLI Type System All types Value types (allocated in-place[with exceptions]) Reference types (allocated on managed heap) Pointers Structures Classes(e.g. strings) Interfaces Enumerations Arrays Delegates User defined structures Simple types (Int32, Int64, Double, Boolean, Char, …) Nullables

  4. CLI Type Inheritance (Sealed Types) pointers(C#: Type *) System.Object(C# keyword: object) interfaces(C# keyword: interface) System.String(C# keyword: string) System.Array System.Delegate sealed arrays(C#: Type[]or Type[,]) System.MulticastDelegate sealed System.ValueType delegates(C# keyword: delegate) user-defined classes(C# keyword: class) sealed Optionally sealed simple types System.Int32(C# keyword: int) System.Int64(C# keyword: long) System.Double(C# keyword: double) sealed System.Nullable(C#: Type?) user-defined structures(C# keyword: struct) System.Enum sealed System.Boolean(C# keyword: bool) sealed enumerations(C# keyword: enum) … sealed

  5. Simple Types (are value types) Compare: in-place size of reference types is platform dependent, e.g. a string variable on a 32-bit platform = 4 B, but on a 64-bit platform = 8 B

  6. Simple Types (are value types) 7 decimal digit precision 15 decimal digit precision 28 decimal digit precision

  7. Simple Types: Implicit Conversions double long short byte int float decimal ulong ushort sbyte uint char All other conversions between types above are possible using an explicit conversion: A B A B A (A) B e.g.: long a = 1; intb = (int) a; C# vs. C/C++: No conversions possible to or from bool type in C#! bool

  8. Simple Types: Implicit Conversions ≠ Inheritance double long short byte int float decimal ulong ushort sbyte uint char All other conversions between types above are possible using an explicit conversion: A B A B A (A) B e.g.: long a = 1; intb = (int) a; short is convertible to int≠int is inherited from shortnorshort is inherited from int

  9. Simple Types: Literals • Integer literals • Default type of integer literals is int– larger type is selected if literal’s value does not fit into an int, e.g. 1000000000000 long • A literal can be implicitly converted to a smaller type if literal’s value fits, e.g.: byte a = 1; • Can be decimal, e.g. 254 • Can be hexadecimal, e.g. 0xFE • Can be binary since C# 7.0, e.g. 0b10010111 • Can use digit separator since C# 7.0, e.g. 0xABCD_EF_05, or 0b1111_0011_1101_0111 • A literal case-insensitive suffix forces a subset of possible types: • Real literals • Default type of real literals is double . • Format: [123456789].123456789[E[+/-]123], e.g. 10.0, 10E15, 10E-4, .14159 • A literal case-insensitive suffix forces literal type:

  10. Note on Decimals Values in Decimals are not normalized: decimal a = 1; decimal b = 1.0000M; decimal c = 1.00M; decimal d = (decimal) 1.000; Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); Console.WriteLine(d); Console.WriteLine(a == b); Console.WriteLine(a == c); Console.WriteLine(a == d); double a = 1; double b = 1.0000; double c = 1.00; Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c);

  11. What is the behavior of the following program? class Program{ static void Main(string[] args) { int a = 10; int b = 0; L1: int c = a / b; L2: int d = c + 15; L3: Console.WriteLine(d); } }

  12. What is the behavior of the following program? class Program{ static void Main(string[] args) { int a = 10; int b = 0; L1:int c = a / b; L2: int d = c + 15; L3: Console.WriteLine(d); } }

  13. What is the behavior of the following program? class Program{ static void Main(string[] args) { double a = 10; double b = 0; L1: double c = a / b; L2: double d = c + 15; L3: Console.WriteLine(d); } }

  14. What is the behavior of the following program? class Program{ static void Main(string[] args) { double a = 10; double b = 0; L1: double c = a / b; L2: double d = c + 15; L3: Console.WriteLine(d); } }

  15. What is the behavior of the following program? class Program{ static void Main(string[] args) { double a = 10; double b = -0; L1: double c = a / b; L2: double d = c + 15; L3: Console.WriteLine(d); } }

  16. What is the behavior of the following program? class Program{ static void Main(string[] args) { double a = 10; double b = -0; L1: double c = a / b; L2: double d = c + 15; L3: Console.WriteLine(d); } } -0 is an intvalue, which gets converted to 0 of int(there is no negative 0 in interger types), then to 0.0 of double.

  17. What is the behavior of the following program? class Program{ static void Main(string[] args) { double a = 10; double b = -0.0; L1: double c = a / b; L2: double d = c + 15; L3: Console.WriteLine(d); } } -0.0 is a valid and unique double value, distinct from 0.0.

  18. Char Literals & Escape Sequences Char literal: a single character or escape sequence in quotes: 'A' or '\n' or '\x5C'

  19. CLI Type Inheritance pointers(C#: Type *) System.Object(C# keyword: object) interfaces(C# keyword: interface) System.String(C# keyword: string) System.Array System.Delegate arrays(C#: Type[]or Type[,]) System.MulticastDelegate System.ValueType delegates(C# keyword: delegate) user-defined classes(C# keyword: class) simple types System.Int32(C# keyword: int) System.Int64(C# keyword: long) System.Double(C# keyword: double) System.Nullable(C#: Type?) user-defined structures(C# keyword: struct) System.Enum System.Boolean(C# keyword: bool) enumerations(C# keyword: enum) …

  20. CLI Type Inheritance (Nullable Types) Can be assigned a null value pointers(C#: Type *) System.Object(C# keyword: object) interfaces(C# keyword: interface) System.String(C# keyword: string) System.Array System.Delegate arrays(C#: Type[]or Type[,]) System.MulticastDelegate System.ValueType delegates(C# keyword: delegate) user-defined classes(C# keyword: class) simple types System.Int32(C# keyword: int) System.Int64(C# keyword: long) System.Double(C# keyword: double) System.Nullable(C#: Type?) user-defined structures(C# keyword: struct) System.Enum System.Boolean(C# keyword: bool) enumerations(C# keyword: enum) …

  21. Nullable Types int i = 123; int? x; x = i; x = 456; x = null; if (x != null) { int j; j = x; // ERROR j = (int) x; // OK } else { int j = (int) x; // EXCEPTION – InvalidOperationException }

  22. Nullable Types [Serializable] public structNullable<T>where T : struct{ public Nullable ( T value ) public boolHasValue { get; } public T Value { get; } public T GetValueOrDefault () public T GetValueOrDefault (T defaultValue ) … } int? x = 456;int? y = null; x == null null == x // !x.HasValue x != null null != x // x.HasValue int? u = x + y; // (x.HasValue && y.HasValue) ? (x + y) : null

  23. Nullable Types [Serializable] public structNullable<T>where T : struct{ public Nullable ( T value ) public boolHasValue { get; } public T Value { get; } public T GetValueOrDefault () public T GetValueOrDefault (T defaultValue ) … } int? x = 456;int? y = null; x == null null == x // !x.HasValue x != null null != x // x.HasValue int? u = x + y; // (x.HasValue && y.HasValue) ? (x + y) : null int? z = x ?? y; // x.HasValue ? x : y ?? operator can be used with all “nullable” types, i.e. reference types as well

  24. Nullable Types, bool?

  25. CLI Type Inheritance pointers(C#: Type *) System.Object(C# keyword: object) interfaces(C# keyword: interface) System.String(C# keyword: string) System.Array System.Delegate arrays(C#: Type[]or Type[,]) System.MulticastDelegate System.ValueType delegates(C# keyword: delegate) user-defined classes(C# keyword: class) simple types System.Int32(C# keyword: int) System.Int64(C# keyword: long) System.Double(C# keyword: double) System.Nullable(C#: Type?) user-defined structures(C# keyword: struct) System.Enum System.Boolean(C# keyword: bool) enumerations(C# keyword: enum) …

  26. Crossing Value/Reference Type Boundary pointers(C#: Type *) System.Object(C# keyword: object) interfaces(C# keyword: interface) System.String(C# keyword: string) System.Array System.Delegate arrays(C#: Type[]or Type[,]) System.MulticastDelegate System.ValueType delegates(C# keyword: delegate) user-defined classes(C# keyword: class) un/boxing un/boxing simple types un/boxing System.Int32(C# keyword: int) System.Int64(C# keyword: long) System.Double(C# keyword: double) System.Nullable(C#: Type?) user-defined structures(C# keyword: struct) System.Enum un/boxing System.Boolean(C# keyword: bool) enumerations(C# keyword: enum) …

  27. Nullable Types – Boxing and Unboxing [Serializable] public structNullable<T>where T : struct{ public Nullable ( T value ) public boolHasValue { get; } public T Value { get; } public T GetValueOrDefault () public T GetValueOrDefault (T defaultValue ) … } inti = 123; int? x = 456;int? y = null; object o1 = i; // o1 = reference to boxed int 123object o2 = x; // o2 = reference to boxed int 456object o3 = y; // o3 = null int i1 = (int)o1; // i1 = 123int i2 = (int)o2; // i2 = 456int i3 = (int)o3; // Error, System.NullReferenceException int? ni1 = (int?)o1; // ni1 = 123int? ni2 = (int?)o2; // ni2 = 456int? ni3 = (int?)o3; // ni3 = null

  28. Mé předměty o .NET a C# • NPRG035 ZS 2/2 Zk/Z “Jazyk C# a platforma .NET” • Základy jazyka, knihoven a běhového prostředí (pro kvalitní OOP), SW inženýrství • NPRG038 LS 2/2 Zk/Z “Programování pro .NET I“ • Pokročilé možnosti jazyka a knihoven potřebné pro „moderní“ programování: delegáti, vlákna a asynchronní programování, síťování, Reflection, generování kódu, enumerační metody, LINQ to Objects • NPRG057 LS 2/0 Zk “Programování pro .NET II” • „interface s okolím“: bezpečnost (.NET Security), interoperabilita s C++, unsafe kód, Python, hostování CLR v C++, vzdálené volání objektů: Remoting, WCF, databáze, ?WF? • Od AR 2015/2016 zakončeno pouze přehledovou zkouškou bez „zápočtového“ programu • NPRG064 LS 0/2 Z (1 kredit) “Programování uživatelských rozhraní v .NET” • „interface s uživatelem“: WinForms, WPF, ASP.NET WebForms, ASP.NET MVC, lokalizace a globalizace aplikací, programování her a visualizačních aplikací ve 2D a 3D (např. WaveEngine, apod.) • Zápočet za 1 kredit za uznaný zápočtový program z NPRG035 nebo NPRG038 s netriviálním uživatelským rozhraním (všechny 3 letní předměty je principiálně možné studovat paralelně jako navazující jen na zimní NPRG035) !!! POZOR!!! “Praktikum z pokročilého objektového programování” pro NMgr. vyžadujePokročilé programování v C++ a Pokročilé programování v .NET I nebo Java

More Related