C# Types Tom Roeder CS 215 2006fa. Administration CMS is up let me know if you can’t see the course Assignments are posted may not be able to do some.

Slides:



Advertisements
Similar presentations
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Written by: Dr. JJ Shepherd
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.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
COMP171 Data Structure & Algorithm Tutorial 1 TA: M.Y.Chan.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
C# Structs, operator overloading & attributes. Structs ~ Structures Structs are similar to classes: they represent data structures with data and functions.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
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.
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 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Delegates and Events Tom Roeder CS fa. Motivation – Function Pointers Treat functions as first-class objects eg. Scheme: (map myfunc somelist)
Computer Organization and Assembly Language C/C++ Pointers and Memory Allocation.
Arrays and Pointers in C Alan L. Cox
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#
Programming in C# Language Overview
OOP Languages: Java vs C++
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Pointer Data Type and Pointer Variables
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Java2C# Antonio Cisternino Part II. Outline Array types Enum types Value types  differences from classes  boxing and unboxing Delegate types  Base.
C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
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.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
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.
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
Types in programming languages1 What are types, and why do we need them?
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Advanced C# Types Tom Roeder CS fa. From last time out parameters difference is that the callee is required to assign it before returning not the.
Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Object Oriented Software Development 4. C# data types, objects and references.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
1.2 Primitive Data Types and Variables
Introduction to C# By: Abir Ghattas Michel Barakat.
C# E1 CSC 298 Arrays in C#. C# E2 1D arrays  A collection of objects of the same type  array of integers of size 10 int[] a = new int[10];  The size.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Arrays and Indexers Programming in C# Arrays and Indexers CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Andy Wang Object Oriented Programming in C++ COP 3330
Java Primer 1: Types, Classes and Operators
Computing with C# and the .NET Framework
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Introduction to Data Structure
Java Programming Language
Presentation transcript:

C# Types Tom Roeder CS fa

Administration CMS is up let me know if you can’t see the course Assignments are posted may not be able to do some of them yet but you are welcome to hand them in whenever Tom’s A-exam tomorrow 5130 Upson, 2:30

Common Type System From MSDN

Common types Everything in C# inherits from object Complaint: too slow Java reasoning: no need to waste space integer types: signed: sbyte, int, short, long unsigned: byte, uint, ushort, ulong floating point: float, double

Common types string type: string can index like char array has method Split e.g., string s = “Hello”; char third = s[2]; string[] split = s.Split(third);

Common types Default values only for instance variables, static variables, and array elts eg. double x; // x == 0 string f; // f.equals(“”) A a; // a == null what is the difference between double and class A? reference types vs. value types two families of types in C#

Reference Types Normal objects (as in Java) inherit from object refer to a memory location can be set to null very much like pointers in other languages memory } var of class A { A a = new A(); A b = a; } a b

Value Types Contain the actual value, not the location Inherit from System.ValueType treated specially by the runtime: no subclassing not objects in normal case but can become objects on demand memory { int a = 137; int b = a; } a b 137

Boxing and Unboxing Value types not objects performance gain in common case sometimes need to become objects called “boxing”. Reverse is “unboxing” { int a = 137; object o1 = a; object o2 = o1; int b = (int)o2; } memory a b o1 o2 137 int 137

Differences between types Copy semantics:  Polynomial a = new Polynomial(); Polynomial b = a; b.Coefficient[0] = 10; Console.WriteLine(a.Coefficient[0]);  int a = 1; int b = a; b = 10; Console.WriteLine(a); Copies of value types make a real copy important for parameter passing, too boxing still copies

Common Value Types All integer and floating point types Strings Anything that wouldn’t be an object in Java Structs user-defined value types can contain arbitrary data non-extensible (sealed subclasses) examples: Point, TwoDPoint, inheritance

Reference Types All are classes that are subtypes of object single inheritance in class hierarchy implement arbitrarily many interfaces same idea for interfaces as in Java: access patterns note interface naming: IAmAnInterface can be abstract class must be marked as abstract, but no member need be abstract May contain non-method non-data members

Arrays Can have standard C arrays int[] array = new int[30]; int[][] array = new int[2][]; array[0] = new int[100]; array[1] = new int[1]; Called “jagged” arrays stored in random parts of the heap stored in row major order Can have arbitrary dimensions Recall that an array is an object

C# Arrays Multidimensional stored sequentially not specified what order for instance: what is the order for foreach? JIT computes the offset code int[,] array = new int[10,30]; array[3,7] = 137; saves computation for some applications can have arbitrary dimensions

C# Arrays can implement arbitrary storage order with a neat property trick: indexers: public int this[int a, int b] { get { // do calculation to find true location of (a,b) return mat[f(a, b), g(a, b)]; } } Allows “indexing” of an object what sort of object might you want to index?

Properties Recall normal access patterns protected int x; public int GetX(); public void SetX(int newVal); elevated into the language: public int X { get { return x; } set { x = value; } }

Properties Can have three types of property read-write, read-only, write-only note: also have readonly modifier Why properties? can be interface members public int ID { get; }; clean up naming schemes Abstracts many common patterns static and dynamic properties of code; tunable knobs note: in Java, used for function pointers

Indexers Allow bracket notation on any object public string this[int a, double b] { … } Used, eg. in hashtables val = h[key] simplifies notation Related to C++ operator[ ] overloading Special property

Function parameters ref parameters reference to a variable can change the variable passed in out parameters value provided by callee Note: reference types are passed by value so can change underlying object where might we have been able to use this? see example from quiz from last time

Function parameters For variable number of parameters public void f(int x, params char[] ar); call f(1), f(1, ‘s’), f(1, ‘s’, ‘f’), f(1, “sf”.ToCharArray()); explicit array where is this used? example from C: printf Can use object[] to get arbitrary parameters why would we want to avoid this? will box value types

Iterators Common code pattern: walk a data structure want to abstract to a GetNext() walk iterator returns next element in walk can be done explicitly: IDictionaryEnumerator iDictEnum = h.GetEnumerator(); while(iDictEnum.MoveNext()) { object val = iDictEnum.Value; object key = iDictEnum.Key; // do something with the key/value pair }

Iterators C# way  foreach(object key in h.Keys) { object val = h[key]; // do something with the key/value pair } Can do even better with generics (C# 2.0) can know the type of the key then no need to cast now in Java (1.5) too for(Object o: collection) { … }

Iterators Can implement own iterable class must implement IEnumerable: public IEnumerator GetEnumerator() { … } IEnumerator: MoveNext(), Current, Reset() old way (C# 1.1) implement a state machine in an inner class keeps track of where and returns next tedious and error prone

C# 2.0 Iterators Major change: yield return compiler builds the inner class eg. public IEnumerator GetEnumerator() { for(int i = 0; i < ar.Length; i++) { yield return ar[i]; } } Also have yield break limited form of co-routines

Comparators Sort method on many containers provides efficient sorting needs to be able to compare to objects Solution: IComparer public class ArrivalComparer: IComparer { public ArrivalComparer() {} public int Compare(object x, object y) { return ((Process)x).Arrival.CompareTo(((Process)y).Arrival); } } Can then call sortedList.Sort(new ArrivalComparer());

Assignment 1 Write a file transpose operator actually can be useful utility Write a C# class that takes as input regular, puntuated English text returns the transpose Use C# style much of what we discussed today will be useful should be short

Exercise Write a matrix class that has lexicographic sorting recall lexicographic sorting: (i, j) < (i’, j’) means i < i’ or (i=i’ and j < j’)