Csharp/CSharp Tutorial/Data Type/Nullable — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 18:31, 26 мая 2010
Содержание
- 1 A nullable type
- 2 Assign value to nullable int
- 3 Nullable long
- 4 Nullable Struct
- 5 Nullable Types Access: Explicitly use properties
- 6 Nullable Types Access: shortcut syntax
- 7 Nullable Types Assignment
- 8 Null Coalescing Operator
- 9 null unification
- 10 Use nullable objects in expressions: result contains null, because one operand is null
- 11 Using ??
A nullable type
<source lang="csharp">using System;
class MainClass{
public static void Main() { int? count = null; if(count.HasValue) Console.WriteLine("count has this value: " + count.Value); else Console.WriteLine("count has no value"); count = 100; if(count.HasValue) Console.WriteLine("count has this value: " + count.Value); else Console.WriteLine("count has no value"); }
}</source>
count has no value count has this value: 100
Assign value to nullable int
<source lang="csharp">using System;
class MainClass {
public static void Main() { int? count = null; int? result = null; int incr = 10; count = 100; result = count + incr; if(result.HasValue) Console.WriteLine("result has this value: " + result.Value); else Console.WriteLine("result has no value"); }
}</source>
result has this value: 110
Nullable long
<source lang="csharp">using System; public class Employee {
public Employee( string Name ) { this.firstName = firstName; this.terminationDate = null; this.ssn = default(Nullable<long>); } public string firstName; public Nullable<DateTime> terminationDate; public long? ssn;
} public class MainClass {
static void Main() { Employee emp = new Employee( "A"); emp.ssn = 1234567890; Console.WriteLine( "{0} {1}", emp.firstName); if( emp.terminationDate.HasValue ) { Console.WriteLine( "Start Date: {0}", emp.terminationDate ); } long tempSSN = emp.ssn ?? -1; Console.WriteLine( "SSN: {0}", tempSSN ); }
}</source>
Nullable Struct
<source lang="csharp">using System; struct Point {
public int x; public int y; public Point(int xVal, int yVal) { x = xVal; y = yVal; }
} class MainClass {
static void Main() { Point p = new Point(6, 11); Point? nullablePoint = new Point(5, 10); Console.WriteLine("p.x: {0}", p.x); Console.WriteLine("p.y: {0}", p.y); Console.WriteLine("nullablePoint.x: {0}", nullablePoint.Value.x); Console.WriteLine("nullablePoint.y: {0}", nullablePoint.Value.y); }
}</source>
p.x: 6 p.y: 11 nullablePoint.x: 5 nullablePoint.y: 10
Nullable Types Access: Explicitly use properties
<source lang="csharp">using System; class MainClass {
static void Main() { int? nullableInteger = 15; if (nullableInteger.HasValue) Console.WriteLine("{0}", nullableInteger.Value); }
}</source>
15
Nullable Types Access: shortcut syntax
<source lang="csharp">using System; class MainClass {
static void Main() { int? nullableInteger = 15; if (nullableInteger != null) Console.WriteLine("{0}", nullableInteger); }
}</source>
15
Nullable Types Assignment
<source lang="csharp">using System; class MainClass {
static void Main() { int? nullableInteger1, nullableInteger2, nullableInteger3; nullableInteger1 = 28; nullableInteger2 = nullableInteger1; nullableInteger3 = null; Console.WriteLine("nullableInteger1: {0}, nullableInteger2: {1}", nullableInteger1, nullableInteger2); Console.WriteLine("nullableInteger3 {0} null", nullableInteger3 == null ? "is" : "is not"); }
}</source>
nullableInteger1: 28, nullableInteger2: 28 nullableInteger3 is null
Null Coalescing Operator
<source lang="csharp">using System; class MainClass {
static void Main() { int? nullableInteger = null; Console.WriteLine("nullableInteger: {0}", nullableInteger ?? -1); nullableInteger = 10; Console.WriteLine("nullableInteger: {0}", nullableInteger ?? -1); }
}</source>
nullableInteger: -1 nullableInteger: 10
null unification
<source lang="csharp">using System; using System.Collections.Generic; using System.Reflection; using System.Runtime.InteropServices; public class MainClass {
public static void Main(){ int? x1 = null; int? x2 = new int?(); int? y1 = 55; int? y2 = new int?(55); Console.WriteLine("{0}...{1}...{2}...{3}", x1, x2, y1, y2); Console.WriteLine("x1 == null? {0}", x1 == null); object x1o = x1; Console.WriteLine("x1o == null? {0}", x1o == null); Console.WriteLine("{0}...{1}", x1.GetType(), x1o.GetType()); }
}</source>
......55...55 x1 == null? True x1o == null? True Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an ob ject. at MainClass.Main()
Use nullable objects in expressions: result contains null, because one operand is null
<source lang="csharp">using System;
class MainClass {
public static void Main() { int? count = null; int? result = null; int incr = 10; result = count + incr; if(result.HasValue) Console.WriteLine("result has this value: " + result.Value); else Console.WriteLine("result has no value"); }
}</source>
result has no value
Using ??
<source lang="csharp">using System;
class MainClass {
static double myMethod() { Console.WriteLine("In myMethod()."); return 0.0; } public static void Main() { double? defaultValue = 1.5; double currentBalance; currentBalance = defaultValue ?? myMethod(); Console.WriteLine(currentBalance); defaultValue = null; currentBalance = defaultValue ?? myMethod(); Console.WriteLine(currentBalance); }
}</source>
1.5 In myMethod(). 0