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.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 6 th Lecture Pavel Ježek
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
Language Fundamentals in brief C# - Introduction.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Constants and Data Types Constants Data Types Reading for this class: L&L,
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.
Data Types and Expressions
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Java Syntax Primitive data types Operators Control statements.
4. Statements and Methods. 2 Microsoft Objectives “With regards to programming statements and methods, C# offers what you would come to expect from a.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Programming in C# Language Overview
Performing Simple Calculations with C# Svetlin Nakov Telerik Corporation
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
.NET Data types. Introduction ٭ A "data type" is a class that is primarily used just to hold data. ٭ This is different from most other classes since they're.
1. 2 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Decisions { class.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.
C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.
Spring 2007NOEA: Computer Science Programme 1 C# - Introduction Language Fundamentals: Data Types string Objects and Classes Methods Iteration and Selection.
C# Intro Programming languages and programs: Source code and object code Editors and compilers C# fundamentals: Program structure Classes and Objects Variables.
C#C# Introduction CS3260 Dennis A. Fairclough Version 1.0 Introduction CS3260 Dennis A. Fairclough Version 1.0.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)
Chapter 2: Using Data.
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
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,
Copyright Curt Hill Variables What are they? Why do we need them?
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Chapters 2 & 3. .NET Software development model that allows applications created in disparate programming languages to communicate Universal data access.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 8 th Lecture Pavel Ježek
CSI 3125, Preliminaries, page 1 Data Type, Variables.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
CNS 3260 C# .NET Software Development
1.2 Primitive Data Types and Variables
Introduction to C# By: Abir Ghattas Michel Barakat.
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
INTRODUCTION CHAPTER #1 Visual Basic.NET. VB.Net General features It is an object oriented language  In the past VB had objects but focus was not placed.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Object Oriented Programming Lecture 2: BallWorld.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Java Programming Language Lecture27- An Introduction.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Basic Introduction to C#
Primitive/Reference Types and Value Semantics
Java Primer 1: Types, Classes and Operators
Computing with C# and the .NET Framework
Chapter 3: Understanding C# Language Fundamentals
Chapter 2.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
An overview of Java, Data types and variables
Data Types Imran Rashid CTO at ManiWeber Technologies.
Java Programming Language
Java Basics Data Types in Java.
Review of Java Fundamentals
Presentation transcript:

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 languages, to work together. To ensure interoperability across languages, Microsoft has also defined the CLS, or Common Language Specification, a subset of the CTS that all languages support. Otherwise, the types in C# are what you would expect from a modern OOPL…” The Common Type System Value vs. reference types Arrays Namespaces

3 Microsoft Part 1 The Common Type System…

4 Microsoft The Common Type System (CTS) CTS is based on a hierarchy of classes defined in FCL –all types inherit from Object (all except interface types)

5 Microsoft The Common Language Specification (CLS) Not all languages support all CTS types and features –C# supports unsigned integer types, VB.NET does not –C# is case sensitive, VB.NET is not –C# supports pointer types (in unsafe mode), VB.NET does not –C# supports operator overloading, VB.NET does not CLS was drafted to promote language interoperability –vast majority of classes within FCL are CLS-compliant

6 Microsoft Mapping C# to CTS Language keywords map to common CTS classes: KeywordDescriptionSpecial format for literals bool Boolean true false char 16 bit Unicode character 'A' '\x0041' '\u0041' sbyte 8 bit signed integernone byte 8 bit unsigned integernone short 16 bit signed integernone ushort 16 bit unsigned integernone int 32 bit signed integernone uint 32 bit unsigned integer U suffix long 64 bit signed integer L or l suffix ulong 64 bit unsigned integer U/u and L/l suffix float 32 bit floating point F or f suffix double 64 bit floating pointno suffix decimal 128 bit high precision M or m suffix string character sequence

7 Microsoft Example An example of using types in C# –declare before you use (compiler enforced) –initialize before you use (compiler enforced) public class App { public static void Main() { int width, height; width = 2; height = 4; int area = width * height; int x; int y = x * 2;... } declarations decl + initializer error, x not set

8 Microsoft Type conversion Some automatic type conversions available –from smaller to larger types Otherwise you need a cast or an explicit conversion… –typecast syntax is type name inside parentheses –conversion based on System.Convert class int i = 5; double d = 3.2; string s = "496"; d = i; i = (int) d; i = System.Convert.ToInt32(s); implicit conversion typecast required conversion required

9 Microsoft Part 2 Value vs. reference types…

10 Microsoft Value vs. reference types C# separates data types into two categories Value types: –variable represents a value ("bits") Reference types: –variable represents a reference to a heap-based object –actual data resides in the object int i; i = 10; 10 string s; s = "calico"; "calico"

11 Microsoft How do you know which types are which? Memorization! Though it's pretty obvious based on past experience –primitive types like bool, int and double are values –remainder are reference types int i; string s; Customer c1, c2; i = 23; s = "a message"; c1 = null; c2 = new Customer(…);

12 Microsoft Boxing and Unboxing When necessary, C# will auto-convert value object –value ==> object is called "boxing" –object ==> value is called "unboxing" int i, j; object obj; string s; i = 32; obj = i; // boxed copy! i = 19; j = (int) obj; // unboxed! s = j.ToString(); // boxed! s = 99.ToString(); // boxed!

13 Microsoft User-defined reference types Classes! –for example, Customer class we worked with earlier… public class Customer { public string Name; // fields public int ID; public Customer(string name, int id) // constructor { this.Name = name; this.ID = id; } public override string ToString() // method { return "Customer: " + this.Name; } }

14 Microsoft Working with reference types… Creating, assigning, and comparing: Customer c1, c2, c3; string s1, s2; c1 = new Customer("joe hummel", 36259); c2 = new Customer("marybeth lore", 55298); c3 = null; // c3 references no object c3 = c1; // c3 now references same obj as c1 if (c1 == null)... // do I ref an object? if (c1 == c2)... // compares references if (c1.Equals(c2))... // compares objects if (s1 == s2)... // exception: == overloaded to // compare string data

15 Microsoft Defining equality Classes should override Equals public class Customer {. public override bool Equals(object obj) { Customer other; if ((obj == null) || (!(obj is Customer))) return false; // definitely not equal other = (Customer) obj; // typecast to access return this.ID == other.ID; // equal if same id... }

16 Microsoft GetHashCode If you override Equals, must also override GetHashCode: public class Customer {. public override int GetHashCode() { return this.id.GetHashCode(); }

17 Microsoft Part 3 Arrays…

18 Microsoft Arrays Arrays are reference types –based on Array class in FCL –must be created using new –0-based indexing –assigned default values (0 for numeric, null for references, etc.) int[] a; a = new int[5]; a[0] = 17; a[1] = 32; int x = a[0] + a[1] + a[4]; int l = a.Length; element access create number of elements

19 Microsoft Multi-dimensional arrays C# supports arrays as a single object OR array of arrays –latter allows you to implement jagged arrays Customer[,] twoD; int[][] jagged2D; // 2D array as single object twoD = new Customer[10, 100]; twoD[0, 0] = new Customer(…); twoD[9, 99] = new Customer(…); // 2D array as array of arrays jagged2D = new int[10][]; jagged2D[0] = new int[10]; jagged2D[1] = new int[20]; jagged2D[9] = new int[100]; jagged2D[0][0] = 1; jagged2D[9][99] = 100;

20 Microsoft Part 4 Namespaces…

21 Microsoft Namespaces Namespaces are a means for organizing types –a namespace N is a set of names scoped by N –namespaces are often nested namespace Workshop { public class Customer {. } public class Product {. } }//namespace Workshop.Customer

22 Microsoft Example Framework Class Library (FCL) contains 1000's of classes –how to organize? –how to avoid name collisions? with FCL within FCL

23 Microsoft FCL namespaces FCL's outermost namespace is "System" FCL technologies nested within System… NamespacePurposeAssembly SystemCore classes, typesmscorlib.dll System.CollectionsData structuresmscorlib.dll System.DataDatabase accessSystem.Data.dll System.Windows.FormsGUISystem.Windows.Forms.dll System.XMLXML processingSystem.Xml.dll

24 Microsoft Namespace != Assembly Orthogonal concepts: –namespace for organization –assembly for packaging One namespace could be spread across multiple assemblies One assembly may contain multiple namesspaces –e.g. mscorlib.dll

25 Microsoft Summary CTS is the common type system –same type system for all languages –types implemented by classes in FCL –fundamental difference between value & reference types CLS is the common language specification –types that are guaranteed to work across languages Try not to confuse namespaces with assemblies… –namespaces help with organization –assemblies denote implementation / packaging

26 Microsoft References Books: –I. Pohl, "C# by Dissection" –S. Lippman, "C# Primer" –J. Mayo, "C# Unleashed"