Interfaces, Enumerations, Boxing, and Unboxing

Slides:



Advertisements
Similar presentations
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
Advertisements

C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#
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#
Overview of C# CS331. Structure of a C# Program // Specify namespaces we use classes from here using System; using System.Threading; // Specify more specific.
Java2C# Antonio Cisternino Part II. Outline Array types Enum types Value types  differences from classes  boxing and unboxing Delegate types  Base.
Introduction to C# C# is - elegant, type-safe, object oriented language enabling to build applications that run on the.NET framework - types of applications.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
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.
Understanding Data Types and Collections Lesson 2.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
CS360 Windows Programming
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
Managing C++ CHRIS DAHLBERG MID-TIER DEVELOPER SCOTTRADE.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
1 9/22/05CS360 Windows Programming Arrays, Collections, Hash Tables, Strings.
Session 1 C# Basics.
Introduction to C# By: Abir Ghattas Michel Barakat.
Introduction to C# Anders Hejlsberg Distinguished Engineer Developer Division Microsoft Corporation.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Seven Lecture Collection 1. 2 Some Legacy Collection Types  ArrayList  Enumeration  Vector  HashTable – self study.
SCJP 5, 1/7 Declarations, Initialization and Scoping
Week 9 - Friday.  What did we talk about last time?  typedef  Linked lists.
CMSC 202 ArrayList Aug 9, 2007.
Creating and Using Objects, Exceptions, Strings
Sixth Lecture ArrayList Abstract Class and Interface
Java Yingcai Xiao.
“Form Ever Follows Function” Louis Henri Sullivan
Elementary Programming
Module 5: Common Type System
Review: Chapter 5: Syntax directed translation
CSE 303 Concepts and Tools for Software Development
Days of the week NEXT.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Days of the Week Les docs d’Estelle –
CS360 Windows Programming
C# In many ways, C# looks similar to Java
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
CS313D: Advanced Programming Language
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
CSC 253 Lecture 8.
DAYS OF THE WEEK.
CSC 253 Lecture 8.
Time management School of Rock.
Sunday Monday Tuesday Wednesday Sunday Monday Tuesday Wednesday
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
CS-161 Computer Programming Lecture 14: Arrays I
CMSC 202 ArrayList Aug 9, 2007.
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
CMSC 202 ArrayList Aug 9, 2007.
Reading Calendar: Hatchet by Gary Paulson
Days of the Week Monday Tuesday Wednesday Friday Thursday Saturday
Reading Calendar: Hatchet by Gary Paulson
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
5. 3 Coding with Denotations
January 2015 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Arrays.
Software Design Lecture : 34.
Time 1.
Contact
NOVEMBER READING LOG Student: When you have read, record your minutes and have your parent initial the proper box (each day). At the end of the month,
2011年 5月 2011年 6月 2011年 7月 2011年 8月 Sunday Monday Tuesday Wednesday

Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Chengyu Sun California State University, Los Angeles
CSE 303 Concepts and Tools for Software Development
Week 7 - Monday CS 121.
Presentation transcript:

Interfaces, Enumerations, Boxing, and Unboxing 9/8/05 CS360 Windows Programming

CS360 Windows Programming Last Week We Started looking at the different types in C# Introduced the concept of the stack and heap Looked at the components of classes Covered the difference between reference and value types 9/8/05 CS360 Windows Programming

CS360 Windows Programming Interfaces Classes that have no fields and the methods have no implementations The implementations appear in the classes that inherit from the interface When a class inherits from an interface it must implement all methods and properties 9/8/05 CS360 Windows Programming

CS360 Windows Programming Interface Example interface INode {  string Text  {  get; set; }  object Tag  {  get; set; }  int Height int Width float CalculateArea();  } 9/8/05 CS360 Windows Programming

CS360 Windows Programming Interfaces Example Write a class (Node) that inherits from INode Class Node must contain the following fields m_text m_height m_width Write the code needed to instantiate your Node class and test all properties and methods 9/8/05 CS360 Windows Programming

CS360 Windows Programming Enumerations Enumerations define literals that are then used as constants for their corresponding values public enum DAYS { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } 9/8/05 CS360 Windows Programming

CS360 Windows Programming Enumerations Cont. By default, the values in an enumeration are 0 for the first item 1 larger for every subsequent item 9/8/05 CS360 Windows Programming

CS360 Windows Programming Enumerations Cont. class EnumTest { public enum DAYS { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } static void Main(string[] args) Array dayArray = Enum.GetValues(typeof(Class1.DAYS)); foreach (DAYS day in dayArray) Console.WriteLine("Number {1} of EnumTest.DAYS is {0}", day, day.ToString("d")); } 9/8/05 CS360 Windows Programming

CS360 Windows Programming Enumerations Cont. 9/8/05 CS360 Windows Programming

CS360 Windows Programming Enumerations Cont. public enum DAYS { Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } 9/8/05 CS360 Windows Programming

CS360 Windows Programming Delegates We will skip delegates for today 9/8/05 CS360 Windows Programming

CS360 Windows Programming Boxing and Unboxing Why do we have reference and value types? Why not just stick with reference types? Where are each of these created in memory? What happens when we pass a value type to a method that expects a reference type? 9/8/05 CS360 Windows Programming

CS360 Windows Programming Boxing and Unboxing Boxing Creates a copy of the value type on the managed heap Unboxing Duplicates a reference type on the stack 9/8/05 CS360 Windows Programming

CS360 Windows Programming C# Default Values The following are automatically initialized to their default values: Static variables Instance variables of class instances Array elements For value types, the default value is 0 For reference types, the default value is null Note that local variables are not considered to be initially assigned 9/8/05 CS360 Windows Programming

CS360 Windows Programming C# Built-in Classes String ArrayList Stack Queue Hashtable These are found in the namespace System.Collections 9/8/05 CS360 Windows Programming

CS360 Windows Programming Example Stack Heap struct Point { public int x, y; } . . . ArrayList list = new ArrayList(); Point p; for (int i = 0; i < 2; i++) p.x = p.y = i; list.Add(p); list p x = 0 y = 0 x = 0 y = 0 9/8/05 CS360 Windows Programming

CS360 Windows Programming Example Stack Heap struct Point { public int x, y; } . . . ArrayList list = new ArrayList(); Point p; for (int i = 0; i < 2; i++) p.x = p.y = i; list.Add(p); list p x = 0 y = 0 x = 1 y = 1 x = 1 y = 1 Boxing 9/8/05 CS360 Windows Programming

CS360 Windows Programming Boxing and Unboxing C# hides the boxing from us We don’t have to change the syntax of our programs However, when unboxing, an explicit type cast is needed 9/8/05 CS360 Windows Programming

CS360 Windows Programming Example Stack Heap struct Point { public int x, y; } . . . ArrayList list = new ArrayList(); Point p; for (int i = 0; i < 2; i++) p.x = p.y = i; list.Add(p); p = (Point) list[0]; list p x = 0 y = 0 x = 0 y = 0 x = 1 y = 1 Unboxing 9/8/05 CS360 Windows Programming

CS360 Windows Programming Summary Completed p. 27 - 38 Next time we will complete chapter 2 We will talk about exception handling 9/8/05 CS360 Windows Programming