Exploring the World of Value Types in C# Programming

slide1 n.w
1 / 30
Embed
Share

Dive into the world of value types in C# programming, from hiding members in subclasses to understanding the CLI Type System and Simple Types. Learn about the differences between value types and reference types, inheritance in C#, and the range of simple types available in the .NET framework.

  • C# Programming
  • Value Types
  • CLI Type System
  • Simple Types
  • Inheritance

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

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 new int x; public new void F() {...} public new void G() {...} } B b = new B(); b.x = ...; b.F(); ... b.G(); // accesses B.x // calls B.F and B.G ((A)b).x = ...; ((A)b).F(); ... ((A)b).G(); // accesses A.x! // 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) sealed System.Array System.Delegate arrays System.MulticastDelegate (C#: Type[] or Type[,]) sealed System.ValueType delegates (C# keyword: delegate) sealed user-defined classes (C# keyword: class) Optionally sealed simple types System.Int32 (C# keyword: int) System.Int64 (C# keyword: long) sealed System.Double (C# keyword: double) System.Boolean (C# keyword: bool) System.Nullable (C#: Type?) sealed System.Enum user-defined structures (C# keyword: struct) sealed enumerations (C# keyword: enum) sealed

  5. Simple Types (are value types) .NET Type C# Keyword CLS Compliant In-place Size in Bytes (bits) Range System.Byte byte Yes 1 B (8 b) 0 255 System.SByte sbyte - 1 B (8 b) -128 127 System.UInt16 ushort - 2 B (16 b) 0 65,535 System.Int16 short Yes 2 B (16 b) -32,768 32,767 System.UInt32 uint - 4 B (32 b) 0 4,294,967,295 System.Int32 int Yes 4 B (32 b) -2,147,483,648 2,147,483,647 System.UInt64 ulong - 8 B (64 b) 0 18,446,744,073,709,551,615 System.Int64 long Yes 8 B (64 b) -9,223,372,036,854,775,808 9,223,372,036,854,775,807 System.Single float Yes 4 B (32 b) IEEE 754: 1-bit sign + 23(+1)-bit mantissa + 8-bit signed exponent: 3.402823 * 1038 System.Double double Yes 8 B (64 b) IEEE 754: 1-bit sign + 52(+1)-bit mantissa + 11-bit signed exponent: 1.79769313486232 * 10308 System.Boolean bool Yes - true, false 1-bit sign + 96-bit integer / 100-28, i.e.: 296/ 100-28 System.Decimal decimal Yes 16 B (128 b) System.Char char Yes 2 B (16 b) UTF-16 characters (Unicode) 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) .NET Type C# Keyword CLS Compliant In-place Size in Bytes (bits) Range System.Byte byte Yes 1 B (8 b) 0 255 System.SByte sbyte - 1 B (8 b) -128 127 System.UInt16 ushort - 2 B (16 b) 0 65,535 System.Int16 short Yes 2 B (16 b) -32,768 32,767 System.UInt32 uint - 4 B (32 b) 0 4,294,967,295 System.Int32 int Yes 4 B (32 b) -2,147,483,648 2,147,483,647 System.UInt64 ulong - 8 B (64 b) 0 18,446,744,073,709,551,615 System.Int64 long Yes 8 B (64 b) -9,223,372,036,854,775,808 9,223,372,036,854,775,807 System.Single float Yes 4 B (32 b) IEEE 754: 1-bit sign + 23(+1)-bit mantissa + 8-bit signed exponent: 3.402823 * 1038 7 decimal digit precision System.Double double Yes 8 B (64 b) IEEE 754: 1-bit sign + 52(+1)-bit mantissa + 11-bit signed exponent: 1.79769313486232 * 10308 15 decimal digit precision System.Boolean bool Yes - true, false 1-bit sign + 96-bit integer / 100-28, i.e.: 296/ 100-28 System.Decimal decimal Yes 16 B (128 b) 28 decimal digit precision System.Char char Yes 2 B (16 b) UTF-16 characters (Unicode)

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

  8. Simple Types: Implicit Conversions Inheritance double byte short int long float sbyte ushort ulong decimal uint All other conversions between types above are possible using an explicit conversion: char A A B A (A) B B e.g.: long a = 1; int b = (int) a; short is convertible to int int is inherited from short nor short 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: u U uint, ulong long, ulong l L ul uL Ul UL lu lU Lu LU ulong 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: f F float d D double m M decimal

  10. Note on Decimals .NET Type C# Keyword CLS Compliant In-place Size in Bytes (bits) Range System.Byte byte Yes 1 B (8 b) 0 255 System.SByte Values in Decimals are not normalized: sbyte - 1 B (8 b) -128 127 double a = 1; double b = 1.0000; double c = 1.00; System.UInt16 decimal a = 1; decimal b = 1.0000M; decimal c = 1.00M; decimal d = (decimal) 1.000; ushort - 2 B (16 b) 0 65,535 System.Int16 short Yes 2 B (16 b) -32,768 32,767 System.UInt32 uint - 4 B (32 b) 0 4,294,967,295 Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); System.Int32 int Yes 4 B (32 b) -2,147,483,648 2,147,483,647 System.UInt64 Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); Console.WriteLine(d); ulong - 8 B (64 b) 0 18,446,744,073,709,551,615 System.Int64 long Yes 8 B (64 b) -9,223,372,036,854,775,808 9,223,372,036,854,775,807 System.Single float Yes 4 B (32 b) IEEE 754: 1-bit sign + 23(+1)-bit mantissa + 8-bit signed exponent: 3.402823 * 1038 Console.WriteLine(a == b); System.Double Console.WriteLine(a == c); Console.WriteLine(a == d); double Yes 8 B (64 b) IEEE 754: 1-bit sign + 52(+1)-bit mantissa + 11-bit signed exponent: 1.79769313486232 * 10308 System.Boolean bool Yes - true, false 1-bit sign + 96-bit integer / 100-28, i.e.: 296/ 100-28 System.Decimal decimal Yes 16 B (128 b) System.Char char Yes 2 B (16 b) UTF-16 characters (Unicode)

  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); } } Option Result A It will not compile. B It will generate a runtime error at line L1. C It will generate a runtime error at line L2. D It will generate a runtime error at line L3. E It will print something to the standard output.

  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); } } Option Result A It will not compile. B It will generate a runtime error at line L1 DivideByZeroException. C It will generate a runtime error at line L2. D It will generate a runtime error at line L3. E It will print something to the standard output.

  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); } } Option Result A It will not compile. B It will generate a runtime error at line L1. C It will generate a runtime error at line L2. D It will generate a runtime error at line L3. E It will print something to the standard output.

  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); } } Option Result A It will not compile. B It will generate a runtime error at line L1. C It will generate a runtime error at line L2. D It will generate a runtime error at line L3. E It will print something to the standard output: Infinity or +nekone no in Czech locale (see double.PositiveInfinity, double.NegativeInfinity, double.NaN).

  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); } } Option Result A It will not compile. B It will generate a runtime error at line L1. C It will generate a runtime error at line L2. D It will generate a runtime error at line L3. E It will print something to the standard output.

  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 int value, which gets converted to 0 of int (there is no negative 0 in interger types), then to 0.0 of double. Option Result A It will not compile. B It will generate a runtime error at line L1. C It will generate a runtime error at line L2. D It will generate a runtime error at line L3. E It will print something to the standard output: Infinity or +nekone no in Czech locale (see double.PositiveInfinity, double.NegativeInfinity, double.NaN).

  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. Option Result A It will not compile. B It will generate a runtime error at line L1. C It will generate a runtime error at line L2. D It will generate a runtime error at line L3. E It will print something to the standard output: -Infinity or -nekone no in Czech locale (see double.PositiveInfinity, double.NegativeInfinity, double.NaN).

  18. Char Literals & Escape Sequences Char literal: a single character or escape sequence in quotes: 'A' or '\n' or '\x5C' Escape Sequence Character Unicode Code (in hex) \' ' 27 \" " 22 \\ \ 5C \0 Null 0 \b Backspace 8 \n New line A \r Carriage return D \t Tab (horizontal) 9 \xWXYZ Any character WXYZ \a Alert 7 \f Form feed C \v Vertical tab B

  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 System.MulticastDelegate (C#: Type[] or Type[,]) 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.Boolean (C# keyword: bool) System.Nullable (C#: Type?) System.Enum user-defined structures (C# keyword: struct) enumerations (C# keyword: enum)

  20. CLI Type Inheritance (Nullable Types) pointers (C#: Type *) Can be assigned a null value System.Object (C# keyword: object) interfaces (C# keyword: interface) System.String (C# keyword: string) System.Array System.Delegate arrays System.MulticastDelegate (C#: Type[] or Type[,]) 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.Boolean (C# keyword: bool) System.Nullable (C#: Type?) System.Enum user-defined structures (C# keyword: struct) 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 struct Nullable<T> where T : struct { public Nullable ( T value ) public bool HasValue { 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 struct Nullable<T> where T : struct { public Nullable ( T value ) public bool HasValue { 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? x Y x & y true false null false false false null false null x | y true true true true false null true null null true true true false false false null null null true false null true false null true false null

  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 System.MulticastDelegate (C#: Type[] or Type[,]) 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.Boolean (C# keyword: bool) System.Nullable (C#: Type?) System.Enum user-defined structures (C# keyword: struct) 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 System.MulticastDelegate (C#: Type[] or Type[,]) 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.Boolean (C# keyword: bool) System.Nullable (C#: Type?) System.Enum user-defined structures (C# keyword: struct) un/boxing enumerations (C# keyword: enum)

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

  28. M pedmty 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 chaplikac ve 2D a 3D (nap . WaveEngine, apod.) Z po etza 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 aduje Pokro il programov n v C++ a Pokro il programov n v .NET I nebo Java

Related


More Related Content