Learn C# Errata. 3-9 The Nullable Types The Assignment Operators

Similar documents
CS2141 Software Development using C/C++ C++ Basics

JAVA Programming Fundamentals

Introduction to C# Applications

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

DigiPen Institute of Technology

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Unit 3. Operators. School of Science and Technology INTRODUCTION

CS111: PROGRAMMING LANGUAGE II

.Net Technologies. Components of.net Framework

Introduction to Programming Using Java (98-388)

Object Oriented Programming in C#

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Chapter 1 Getting Started

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Operators and Expressions

CS260 Intro to Java & Android 03.Java Language Basics

Chapter 3: Operators, Expressions and Type Conversion

Equality in.net. Gregory Adam 07/12/2008. This article describes how equality works in.net

Java Programming Fundamentals. Visit for more.

JAVA OPERATORS GENERAL

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Chapter 2: Using Data

Java Primer 1: Types, Classes and Operators

1. Find the output of following java program. class MainClass { public static void main (String arg[])

CS Programming I: Primitives and Expressions

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Expression and Operator

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Chapter 6. Simple C# Programs

Programming overview

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CIS133J. Working with Numbers in Java

Java and C# in Depth Carlo A. Furia, Marco Piccioni, Bertrand Meyer

C++ Programming: From Problem Analysis to Program Design, Third Edition

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

PHY4321 Summary Notes

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Visual C# Instructor s Manual Table of Contents

A Comparison of Visual Basic.NET and C#

CHAD Language Reference Manual

Expressions and Casting

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lexical Considerations

Introduction to C++ with content from

The C Programming Language Guide for the Robot Course work Module

Object-Oriented Programming in C# (VS 2015)

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Simple Java Reference

Reserved Words and Identifiers

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Previous C# Releases. C# 3.0 Language Features. C# 3.0 Features. C# 3.0 Orcas. Local Variables. Language Integrated Query 3/23/2007

: Principles of Imperative Computation Victor Adamchik. Practice Exam - I

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

Programming. Elementary Concepts

Object Oriented Programming

Midterm Exam CS 251, Intermediate Programming October 8, 2014

Preview from Notesale.co.uk Page 3 of 36

Computer Science II (20082) Week 1: Review and Inheritance

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

CS110: PROGRAMMING LANGUAGE I

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Objectives. In this chapter, you will:

Operators. Java operators are classified into three categories:

Industrial Programming

BASIC ELEMENTS OF A COMPUTER PROGRAM

CS304 Object Oriented Programming Final Term

Introduction To C#.NET

Outline. More optimizations for our interpreter. Types for objects

Whidbey Enhancements to C# Jeff Vaughan MSBuild Team July 21, 2004

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

DC69 C# &.NET DEC 2015

CMSC 132: Object-Oriented Programming II

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

CS 61C: Great Ideas in Computer Architecture Introduction to C

Zheng-Liang Lu Java Programming 45 / 79

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Types, Operators and Expressions

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

SECTION II: LANGUAGE BASICS

Arithmetic and Bitwise Operations on Binary Data

Visual C# 2012 How to Program by Pe ars on Ed uc ati on, Inc. All Ri ght s Re ser ve d.

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Object-Oriented Programming in C# (VS 2012)

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

CSC 1214: Object-Oriented Programming

3. Java - Language Constructs I

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Transcription:

1 The following pages show errors from the original edition, published in July 2008, corrected in red. Future editions of this book will be printed with these corrections. We apologize for any inconvenience these errors have caused. page 40 page 53 3-6-3 The Assignment Operators The = assignment operator is used for assigning a value, expression, or variable to a variable. The following table shows the use of assignment operators combined with arithmetic operators. Table 3-5: The assignment operators Compound Assignment Operator Example Statement Equivalent Statement *= x*=y; x=x*y; /= x/=y; x=x/y; %= x%=y; x=x%y; += x+=y; x=x+y; = x =y; x=x y; As you can see in the above table, each example in the Example Statement column is equivalent to a statement in the Equivalent Statement column. For example, the statement: x*=y; means multiply x by y and store the result in x. The statement: x%=y; means divide x by y and store the remainder of the division in x. 3-9 The Nullable Types The nullable types were added to value types with C# 2005. This feature enables you to assign the value null to a value-type variable. You need this with databases where a variable can assume any value including null. The nullable variable is declared by using the? symbol next to the type name, like this:

2 mytype? myvariable; where mytype is one of the value types including the struct type. It is called the underlying type of the nullable type. For example, consider the following statement: int? myint = null; In this statement, the underlying type is int. This means that the variable myint can accept all the values that can be assigned to int in addition to null, which means not used or empty. In the following example, bool? mybool; mybool can assume one of the values true, false, or null. In databases, this feature is important because a database field may contain null to indicate that the variable is not defined. This is the same concept used with reference types where the value null has been used to indicate that a variable is not initialized. page 68 4-1-2-4 The Bitwise Operators The logical operators & and can be used with integral operands, in which case they compute the bitwise AND and the bitwise OR of their operands. The following examples perform bitwise AND and OR operations on two integers (21 and 4) and display the results in several formats: // Hexadecimal: Console.WriteLine("0x0:x",0x15 0x4); // 0x15 Console.WriteLine("0x0:x",0x15 & 0x4); // 0x4 // Decimal: Console.WriteLine(21 4); // 21 Console.WriteLine(21 & 4); // 4 page 79 Under section 4-6-1-2, in the last code segment: for (i = 0, j = 10; i<=j, i++; j = j-1) should be: for (i = 0, j = 10; i<=j; i++, j = j-1)

3 page 186 page 275 The job of the new modifier here is to hide the member with the same name in the base class. A method that uses the new modifier hides properties, fields, and types with the same name. It also hides the methods with the same signatures. In general, declaring a member in the derived class would hide any members in the base class with the same name. If you declare MyMethod in the above example without the new modifier, it still works, but you will get a compiler warning: 'MyDerivedClass.MyMethod()' hides inherited member 'MyBaseClass.MyMethod()'. Use the new keyword if hiding was intended. 12-1 What Are Generics? Generics, a new feature that was added to C# 2005, enable the programmer to design classes or function members and postpone the definition of types until the class is instantiated. By adding this feature, C# made a big step in code reuse, especially in the field of collections. WhenyouuseacollectionsuchasStack, you store the items as objects because the type object can hold any kind of data. Consider this example where you store some double numbers in a Stack collection: Stack mystack = new Stack (); mystack.push (4.5); mystack.push (2.1); mystack.push (3.2); page 276 When you create this collection, the compiler automatically boxes the double numbers to convert them to the object type. When you retrieve the data, you have to cast the numbers to the double type. For example: foreach(object obj in mystack) Console.WriteLine((double)obj * 3.14);

4 page 331 14-2 Implicitly Typed Local Variables You can declare a variable with the var keyword and let the compiler infer the type from the expression used to initialize the variable. For example: var myvariable = 1.25; var yourvariable = "Hello, World!"; var myarray = new int[] 1, 2, 4, 8, 64 ; The C# compiler can infer that the type of myvariable is double, the type of yourvariable is string, and that of myarray is int. In other words, the three statements above generate the same result as the following statements: double myvariable = 1.25; string yourvariable = "Hello, World!"; int[] myarray = new int[] 1, 2, 4, 8, 64 ; page 341 Example 14-5 // Example 14-5.cs // Anonymous types using System; using System.Collections.Generic; using System.Text; using System.Query; using System.Xml.XLinq; using System.Data.DLinq; namespace AnonTypes class MyClass static void Main(string[] args) // Declare an anonymous type: var obj1 = new Title = "Organic Babies", ISBN = "5-1234-5678-x" ; // Display the contents: Console.WriteLine("Title: 0\nISBN: 1", obj1.title, obj1.isbn); Console.ReadLine();

5 Output: Title: Organic Babies ISBN: 5-1234-5678-x In order to see the class tree, run ILDASM on the executable file generated from the compilation (14-5.exe) by using the following command line: ILDASM 14-5.exe page 344 14-6 Implicitly Typed Arrays The implicitly typed local variables can be extended to declaring arrays. By examining the members of an array in the initialization expression, the compiler can infer the type of the array elements. The following is the regular C# 2.0 code to declare variables of different types: int[] a = new[] 1, 2, 4, 8, 16, 32, 64 ; double[] b = new[] 3, 3.14, 2.7 ; bool[] c = new [] true, false ; string[] d = new[] "Hello,", "world!" ;