C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright.

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Chapter 4 Defining Classes I Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 10 Classes Continued
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
 2006 Pearson Education, Inc. All rights reserved Generics.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 th Lecture Pavel Ježek.
Java Language and SW Dev’t
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
The Java Programming Language
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension.
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Attributes C#.Net Software Development Version 1.0.
Introduction to C# 2.0 An Advanced Look Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces and Inner Classes
More on Objects Mehdi Einali Advanced Programming in Java 1.
Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Chapter 14 Using JavaBeans Components in JSP Documents.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Operators and Cast. Operators Operator Shortcuts.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Classes (Part 1) Lecture 3
Static data members Constructors and Destructors
C# Operator Overloading and Type Conversions
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
University of Central Florida COP 3330 Object Oriented Programming
Classes.
Advanced Programming in Java
Advanced Programming in Java
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 6 Methods: A Deeper Look
Java Classes and Objects
CMSC 202 Classes and Objects.
How to organize and document your classes
Chapter 4 Defining Classes I
Classes, Objects and Methods
Presentation transcript:

C#.Net Development Version 1.0

Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright © by Dennis A. Fairclough all rights reserved.

Nullable Description Nullable types represent values that are possibly unknown. A nullable type supports all values of its underlying type plus an additional null (unknown) state. Any value type can be the underlying type of a nullable type. A nullable type supports the same conversions and operators as its underlying type, but additionally provides null value propagation similar to SQL. Copyright © by Dennis A. Fairclough all rights reserved.

Nullable Datatypes Nullable types are constructed using the ? type modifier. For example, int? is the nullable form of the predefined type int. A nullable type’s underlying type must be a non- nullable value type. int ? nvalue = 123; int ? nvalue0 = null; Copyright © by Dennis A. Fairclough all rights reserved.

Nullable “HasValue” & Others A nullable type is a structure that combines a value of the underlying type with a boolean null indicator. An instance of a nullable type has two public read-only properties: HasValue, of type bool, and Value, of the nullable type’s underlying type. HasValue is true for a non-null instance and false for a null instance. When HasValue is true, the Value property returns the contained value. When HasValue is false, an attempt to access the Value property throws an exception. GetValueOrDefault() returns the value or the default value if null. Copyright © by Dennis A. Fairclough all rights reserved.

Nullable & Lifted Conversions Nullable conversions and lifted conversions permit predefined and user-defined conversions that operate on non-nullable value types to also be used with nullable forms of those types. Likewise, lifted operators permit predefined and user-defined operators that work for non-nullable value types also work for nullable forms of those types. Copyright © by Dennis A. Fairclough all rights reserved.

Examples Some examples of nullable conversions are shown in the following. int i = 123;//int int? x = i;// int --> int? double? y = x;// int? --> double? int? z = (int?)y;// double? --> int? int j = (int)z;// int? --> int A user-defined conversion operator has a lifted form when the source and target types are both non-nullable value types. A ? modifier is added to the source and target types to create the lifted form. Similar to predefined nullable conversions, lifted conversion operators propagate nulls. Copyright © by Dennis A. Fairclough all rights reserved.

?? Operator A new null coalescing operator, ??, is provided. The result of a ?? b is a if a is non-null; otherwise, the result is b. Intuitively, b supplies the value to use when a is null. When a is of a nullable type and b is of a non-nullable type, a ?? b returns a non-nullable value, provided the appropriate implicit conversions exist between the operand types. In the example int? x = GetNullableInt(); int? y = GetNullableInt(); int? z = x ?? y; int i = z ?? -1; the type of x ?? y is int?, but the type of z ?? -1 is int. The latter operation is particularly convenient because it removes the ? from the type and at the same time supplies the default value to use in the null case. The null coalescing operator also works for reference types. The example string s = GetStringValue(); Console.WriteLine(s ?? "Unspecified"); outputs the value of s, or outputs Unspecified if s is null. Copyright © by Dennis A. Fairclough all rights reserved.

Nullable Types Nullable types A nullable type is classified as a value type: value-type: struct-type enum-type struct-type: type-name simple-type nullable-type nullable-type: non-nullable-value-type ? non-nullable-value-type: type The type specified before the ? modifier in a nullable type is called the underlying type of the nullable type. The underlying type of a nullable type can be any non-nullable value type or any type parameter that is constrained to non-nullable value types (that is, any type parameter with a struct constraint). The underlying type of a nullable type cannot be a nullable type or a reference type. For example, int?? and string? are invalid types. A nullable type can represent all values of its underlying type plus an additional null value. The syntax T? is shorthand for System.Nullable, and the two forms can be used interchangeably. Copyright © by Dennis A. Fairclough all rights reserved.

Examples The example below shows several boxing and unboxing operations involving nullable types. int i = 123; int? x = 456; int? y = null; object o1 = i;// o1 = reference to boxed int 123 object o2 = x;// o2 = reference to boxed int 456 object o3 = y;// o3 = null int i1 = (int)o1;// i1 = 123 int i2 = (int)o2;// i2 = 456 int i3 = (int)o3;// Throws System.NullReferenceException int? ni1 = (int?)o1;// ni1 = 123 int? ni2 = (int?)o2;// ni2 = 456 int? ni3 = (int?)o3;// ni3 = null Copyright © by Dennis A. Fairclough all rights reserved.

Partial Classes Partial types While it is good programming practice to maintain all source code for a type in a single file, Sometimes a type becomes large enough that this is an impractical constraint. Furthermore, programmers often use source code generators to produce the initial structure of an application, and then modify the resulting code. Unfortunately, when source code is emitted again sometime in the future, existing modifications are overwritten. Partial types allow classes, structs, and interfaces to be broken into multiple pieces stored in different source files for easier development and maintenance. Additionally, partial types allow separation of machine-generated and user- written parts of types so that it is easier to augment code generated by a tool. A new type modifier, partial, is used when defining a type in multiple parts. The following is an example of a partial class that is implemented in two parts. The two parts may be in different source files, for example because the first part is machine generated by a database mapping tool and the second part is manually authored: Copyright © by Dennis A. Fairclough all rights reserved.

Example – Partial Class public partial class Customer { private int id; private string name; private string address; private List orders; public Customer() {... } } public partial class Customer { public void SubmitOrder(Order order) { orders.Add(order); } public bool HasOutstandingOrders() { return orders.Count > 0; } } When the two parts above are compiled together, the resulting code is the same as if the class had been written as a single unit: public class Customer { private int id; private string name; private string address; private List orders; public Customer() {... } public void SubmitOrder(Order order) { orders.Add(order); } public bool HasOutstandingOrders() { return orders.Count > 0; } } All parts of a partial type must be compiled together such that the parts can be merged at compile-time. Partial types specifically do not allow already compiled types to be extended. Copyright © by Dennis A. Fairclough all rights reserved.

Partial Methods Partial methods Partial methods can be defined in one part of a type declaration and implemented in another. The implementation is optional; if no part implements the partial method, the partial method declaration and all calls to it are removed from the type declaration resulting from the combination of the parts. C# 3.0 Specification Copyright © by Dennis A. Fairclough all rights reserved.

Partial Methods Partial methods cannot define access modifiers, but are implicitly private. Their return type must be void, and their parameters cannot have the out modifier. The identifier partial is recognized as a special keyword in a method declaration only if it appears right before the void type; otherwise it can be used as a normal identifier. A partial method cannot explicitly implement interface methods. C# 3.0 Specification Copyright © by Dennis A. Fairclough all rights reserved.

Partial Methods – C# 3.0 Spec. There are two kinds of partial method declarations: If the body of the method declaration is a semicolon, the declaration is said to be a defining partial method declaration. If the body is given as a block, the declaration is said to be an implementing partial method declaration. Across the parts of a type declaration there can be only one defining partial method declaration with a given signature, and there can be only one implementing partial method declaration with a given signature. If an implementing partial method declaration is given, a corresponding defining partial method declaration must exist, and the declarations must match as specified in the following: The declarations must have the same modifiers (although not necessarily in the same order), method name, number of type parameters and number of parameters. Corresponding parameters in the declarations must have the same modifiers (although not necessarily in the same order) and the same types (modulo differences in type parameter names). Corresponding type parameters in the declarations must have the same constraints (modulo differences in type parameter names). An implementing partial method declaration can appear in the same part as the corresponding defining partial method declaration. Copyright © by Dennis A. Fairclough all rights reserved.

using System; namespace Partial_Methods_101 { class Program { static void Main(string[] args) { Data data = new Data(); //data.Test("Dennis", 75.55); data.TestTest(); } partial class Data { partial void Test(string s, double d); } partial class Data { public void TestTest() { Test("Dennis", 75.55); } partial void Test(string s, double d) { } Copyright © by Dennis A. Fairclough all rights reserved.

What did you learn? ?? Copyright © by Dennis A. Fairclough all rights reserved.