References types and Value types With a very brief introduction to struct and enum Reference types and Value types1.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
Value Types and Reference Types Enumerations and Structures.
Java Syntax Primitive data types Operators Control statements.
Run-Time Storage Organization
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
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#
Collection types Collection types.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Java2C# Antonio Cisternino Part II. Outline Array types Enum types Value types  differences from classes  boxing and unboxing Delegate types  Base.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Interfaces 1. Interfaces are (parts of) contracts Interfaces are contracts between implementers and consumers Consumers: Programmers using a class implementing.
Lecture Contents Arrays and Vectors: Concepts of pointers and references. Pointer declarations and initialization. Pointer Operators. Dynamic Memory Allocation.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Object-Oriented Programming in C++
Chapters 1-5 Review C++ Class. Chapter 1 – the big picture Objects Class Inheritance Reusability Polymorphism and Overloading.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
Generics in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);
Object Oriented Software Development 4. C# data types, objects and references.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Introduction to C# By: Abir Ghattas Michel Barakat.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Introduction to C# Anders Hejlsberg Distinguished Engineer Developer Division Microsoft Corporation.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Chapter Structured Data 11. Combining Data into Structures 11.2.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes.
Lecture 09 Dr. Eng. Ibrahim El-Nahry C# - Structures, Enumerations and Partial Classes.
Minimising memory churn
Creating and Using Objects, Exceptions, Strings
Module 5: Common Type System
CSE 143 Introduction to C++ [Appendix B] 4/11/98.
Delegates/ Anders Børjesson
Structs.
Primitive Types Vs. Reference Types, Strings, Enumerations
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Using Classes and Objects
Namespaces, Typedefs & Enums
Arrays in Java What, why and how Copyright Curt Hill.
Generics in C# / Anders Børjesson
CS2011 Introduction to Programming I Arrays (II)
String Class.
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

References types and Value types With a very brief introduction to struct and enum Reference types and Value types1

Value types Values Value types are (normally) immutable You cannot change the state of an object after it the object has been created Passing a parameter of a value type, means giving a copy to the called method. Some value types Class System.ValueType Base class of all value types Int, bool, double, etc. DateTime, Point, Color Struct { … } Enum { … } Reference types Objects Reference types are (normally) mutable Passing a parameter of a reference type, means giving a reference to the called object Some reference types String (immutable!!) Class Something { … } Arrays Reference types and Value types2

Memory: Heap vs. Stack A running program (called a process) has two sorts of memory allocated Heap Stack Heap Object are allocated on the heap. The garbage collector “cleans” the heap New SomeReferenceType() The new object is allocated on the heap Threads share the heap References to the object on the heap Stack For every method call data is pushed on the stack For every return data is popped of the stack Data: parameters + local variables New SomeValueType() The value is allocated on the stack Threads do not share stack Reference types and Value types3

Struct Similar to a class, but simpler No inheritance etc. Struct Pair { …. } Value type Values are allocated on the stack, not the heap Some struct types from the C# API System.DateTime System.Windows.Point Example: StructVsClass Reference types and Value types4

Enums An Enums is a set of constant values Example declaration: enum Color { Red, Green, Blue }; Color is a type with only 3 legal values Example usage: Color MyColor = Color.Blue; Enum types extends the System.Enum type Some enum types from the C# API System.DayOfWeek Monday, Tuesday, … System.StringComparison Different ways to compare strings: culture, ignore case, etc. Reference types and Value types5

Enums + switch statement Enums are really integers Red = 0, Green = 1, Blue = 2 However, only 3 values are legal Enum types are often used in switch statements Color MyColor = … Switch (MyColor) { case Color.Blue: … break; case Color.Red: … break } Reference types and Value types6

References and further readings MSDN Structs (C# Programming Guide) Bart De Smet: C# 5.0 Unleashed, Sams 2013 Chapter 9 Introducing Types Classes Versus Structs, page MSDN Enumeration Types (C# Programming Guide) Bart De Smet: C# 5.0 Unleashed, Sams 2013 Chapter 11 Fields, Properties, and Indexers An Intermezzo About Enums, page Reference types and Value types7