Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Language Fundamentals in brief C# - Introduction.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Java Software Solutions
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
C# Structs, operator overloading & attributes. Structs ~ Structures Structs are similar to classes: they represent data structures with data and functions.
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
3. Data Types. 2 Microsoft Objectives “.NET is designed around the CTS, or Common Type System. The CTS is what allows assemblies, written in different.
Reference Types. 2 Objectives Introduce reference types –class –array Discuss details of use –declaration –allocation –assignment –null –parameter –aggregation.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Operator Overloading. 2 Objectives Discuss operator overloading –definition –use –advantages –limitations Present type conversion operators.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Windows Programming Using C# Classes & Interfaces.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.
C# Intro Programming languages and programs: Source code and object code Editors and compilers C# fundamentals: Program structure Classes and Objects Variables.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Chapter 8: Arrays.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Advanced Java Programming CS 537 – Data Structures and Algorithms.
C# EMILEE KING. HISTORY OF C# In the late 1990’s Microsoft recognized the need to be able to develop applications that can run on multiple operating system.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
ISBN Object-Oriented Programming Chapter Chapter
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Object Oriented Software Development 4. C# data types, objects and references.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
Introduction to C# By: Abir Ghattas Michel Barakat.
CIS 200 Test 01 Review. Built-In Types Properties  Exposed “Variables” or accessible values of an object  Can have access controlled via scope modifiers.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
C11, Implications of Inheritance
Static data members Constructors and Destructors
Java Primer 1: Types, Classes and Operators
Module 5: Common Type System
Methods Attributes Method Modifiers ‘static’
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Lecture 9 Concepts of Programming Languages
Constructor Overloading
CLASS DEFINITION (FIELDS)
Java Programming Language
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Interfaces, Enumerations, Boxing, and Unboxing
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Value Types

2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct value type –definition –use –advantages –limitations

3 Motivation Programs make heavy use of some data objects –local variables, parameters, loop counters, etc. Important that implementation be efficient –memory allocation –access –memory reclamation void Process() { for (int i = 0; i < ; i++) {... Point p = new Point();... } would be expensive to allocate and reclaim at each iteration

4 Runtime stack Local variables and parameters stored on runtime stack –memory allocated and reclaimed automatically –efficient since memory management overhead is low void Process(int x) { int y; Stock s;... } parameter local int local reference Process x y s... stack

5 Managed heap Reference type instances stored in managed heap –less efficient than stack due to overhead of heap management void Process(int x) { int y; Stock s = new Stock();... } reference type name price shares heap Process x y s... stack

6 Value types Value types contain data directly –not reference/object pair like reference types All value types are derived from library ValueType class –many provided: int, char, TimeSpan, etc. –can define custom: struct, enum ValueType... Object value types

7 Simple types Simple types are library structures in the System namespace –can use structure name or C# alias Int32 i = 4; int j; j = i; structure name C# alias boolBoolean charChar sbyteSByte byteByte shortInt16 ushortUInt16 intInt32 uintUInt32 longInt64 ulongUInt64 floatSingle doubleDouble decimalDecimal Boolean character integer floating point same type so can interoperate

8 Struct Use struct to define custom value type –automatically derived from ValueType ValueType struct Object programmer defined struct

9 Struct abilities struct has fields, constructors, properties, methods, etc. struct Point : IMeasureable { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } public void Scale(int a) { x *= a; y *= a; } public double Length() { return Math.Sqrt(x * x + y * y); } fields constructor properties method interface method

10 Struct limitations Struct has several limitations –does not support inheritance –cannot use variable initializers –cannot write custom default constructor –cannot define a destructor –constructors must explicitly initialize all fields struct Point3D : Point { private int z = -1; ~Point3D() {... }... } error

11 Value type local variable / parameter Value type local variables and parameters stored on stack –efficient to allocate and reclaim memory Process... stack void Process(Point p) { Point q = new Point();... } local variable pxypxy qxyqxy parameter

12 Value type field Reference type may have field of value type –field stored directly in containing object –reduces number of objects allocated in heap void Process() { Rectangle r = new Rectangle();... } reference type heap Process r... stack class Rectangle { Point ll; Point ur;... } value type fields ll x y ur x y

13 Value type array Array of value types contains actual data –each element is default constructed –reduces number of object allocated in heap Point[] polygon = new Point[5]; array of 5 Point s, all default constructed polygon x 0 y 0 x 0 y 0 x 0 y 0 x 0 y 0 x 0 y 0

14 Default constructors Value types all have default constructors that initialize fields –numeric types set to 0 –char set to null character –bool set to false –reference fields set to null Programmer cannot code default constructor –always provided by compiler int j = new int(); Point p = new Point(); j set to 0 p set to (0,0) 0 j x 0 y 0 p

15 Creation Create value type instance implicitly or explicitly using new Explicit creation recommended –invokes constructor so all fields will be initialized Implicit creation does not invoke constructor –fields not initialized and must be assigned to before use void Process() { Point p; Point q = new Point(); } implicit explicit x - y - p x 0 y 0 q

16 Value semantics Value types have value semantics –assignment –parameter passing –method return value –equality testing

17 Assignment Assignment performs memberwise assignment –value of each field is copied Point p = new Point(3, 4); Point q = new Point(); q = p; assign x 3 y 4 q x 3 y 4 p

18 Value parameter Value type can be passed by value –memory allocated for parameter –field values copied –changes made in method affect only local copy Point p = new Point(3, 4); Process(p); pass x 3 y 4 q x 3 y 4 p void Process(Point q) {... } value parameter

19 Reference parameter Value type can be passed ref or out –no copy made –changes made in method affect actual parameter Point p = new Point(3, 4); Process(ref p); pass x 5 y 6 p,q void Process(ref Point q) { q.X = 5; q.Y = 6; } ref parameter changes p

20 Method return value Value type returned from method by value –field values copied Point Add(Point a, Point b) { Point c = new Point(a.X + b.X, a.Y + b.Y); return c; } value copied out of method

21 Equality Equals method used to compare value types –overridden in class ValueType to compare values –no need to override unless can implement more efficiently class ValueType { public override bool Equals(object obj) {... }... } Point p = new Point(1, 2); Point q = new Point(1, 2); if (p.Equals(q))... true, same data

22 Boxing Value types are type compatible with object –value copied into wrapper automatically –called boxing Boxing most useful for temporary storage –values often boxed and stored in data structure –later unboxed and used Point p = new Point(3, 4); object o = p;... boxed o x 3 y 4 x 3 y 4 p

23 Unboxing Extract value type instance from box using cast –System.InvalidCastException thrown if cast fails void Process(object o) { Point q = (Point)o;... } unbox o x 3 y 4 x 3 y 4 q

24 Copy during boxing Boxing copies value into new memory location –changes to original do not affect boxed copy Point p = new Point(3, 4); object o = p; p.y = 5;... value copied into box o x 3 y 4 x 3 y 5 p modify original, boxed copy unchanged

25 Boxing efficiency Boxing incurs runtime overhead –memory for wrapper allocated on managed heap –value copied into box –memory garbage collected after box no longer referenced Point p = new Point(3, 4); object o = p;... boxed heap o x 3 y 4

26 Summary Value types contain data directly –implemented efficiently –have value semantics Value types interoperate with object –boxing occurs automatically –cast required for unboxing –use incurs some runtime overhead Special category called simple value types –provide fundamental data types Struct provides way to define structured value type