Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright."— Presentation transcript:

1 C#.Net Development Version 1.0

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

3 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 © 2008-2011 by Dennis A. Fairclough all rights reserved.

4 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 © 2008-2011 by Dennis A. Fairclough all rights reserved.

5 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 © 2008-2011 by Dennis A. Fairclough all rights reserved.

6 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 © 2008-2011 by Dennis A. Fairclough all rights reserved.

7 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 © 2008-2011 by Dennis A. Fairclough all rights reserved.

8 ?? 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 © 2008-2011 by Dennis A. Fairclough all rights reserved.

9 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 © 2008-2011 by Dennis A. Fairclough all rights reserved.

10 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 © 2008-2011 by Dennis A. Fairclough all rights reserved.

11 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 © 2008-2011 by Dennis A. Fairclough all rights reserved.

12 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 © 2008-2011 by Dennis A. Fairclough all rights reserved.

13 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 © 2008-2011 by Dennis A. Fairclough all rights reserved.

14 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 © 2008-2011 by Dennis A. Fairclough all rights reserved.

15 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 © 2008-2011 by Dennis A. Fairclough all rights reserved.

16 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 © 2008-2011 by Dennis A. Fairclough all rights reserved.

17 What did you learn? ?? Copyright © 2008-2011 by Dennis A. Fairclough all rights reserved.


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

Similar presentations


Ads by Google