5
5
6
6
namespace CsProgGuideTypes
7
7
{
8
- //-------------------------------------------------------------------------
9
- class WrapCasting
10
- {
11
- //<Snippet1>
12
- static void TestCasting ( )
13
- {
14
- int i = 10 ;
15
- float f = 0 ;
16
- f = i ; // An implicit conversion, no data will be lost.
17
- f = 0.5F ;
18
- i = ( int ) f ; // An explicit conversion. Information will be lost.
19
- }
20
- //</Snippet1>
21
-
22
- }
23
-
24
- //-------------------------------------------------------------------------
25
- class UsingNullable
26
- {
27
- static void Main ( )
28
- {
29
- //<Snippet4>
30
- int ? i = 10 ;
31
- double ? d1 = 3.14 ;
32
- bool ? flag = null ;
33
- char ? letter = 'a' ;
34
- int ? [ ] arr = new int ? [ 10 ] ;
35
- //</Snippet4>
36
-
37
- // Clear compiler warnings:
38
- System . Console . WriteLine ( i ) ;
39
- System . Console . WriteLine ( d1 ) ;
40
- System . Console . WriteLine ( flag ) ;
41
- System . Console . WriteLine ( letter ) ;
42
-
43
- //<Snippet5>
44
- int ? x = 10 ;
45
- if ( x . HasValue )
46
- {
47
- System . Console . WriteLine ( x . Value ) ;
48
- }
49
- else
50
- {
51
- System . Console . WriteLine ( "Undefined" ) ;
52
- }
53
- //</Snippet5>
54
-
55
- //<Snippet6>
56
- int ? y = 10 ;
57
- if ( y != null )
58
- {
59
- System . Console . WriteLine ( y . Value ) ;
60
- }
61
- else
62
- {
63
- System . Console . WriteLine ( "Undefined" ) ;
64
- }
65
- //</Snippet6>
66
-
67
- //<Snippet8>
68
- int ? n1 = null ;
69
- //</Snippet8>
70
- System . Console . WriteLine ( n1 ) ;
71
-
72
- //<Snippet9>
73
- int ? n2 ;
74
- n2 = 10 ; // Implicit conversion.
75
- //</Snippet9>
76
- System . Console . WriteLine ( n2 ) ;
77
-
78
- //<Snippet10>
79
- int ? a = 10 ;
80
- int ? b = null ;
81
-
82
- a ++ ; // Increment by 1, now a is 11.
83
- a = a * 10 ; // Multiply by 10, now a is 110.
84
- a = a + b ; // Add b, now a is null.
85
- //</Snippet10>
86
-
87
- //<Snippet11>
88
- int ? num1 = 10 ;
89
- int ? num2 = null ;
90
- if ( num1 >= num2 )
91
- {
92
- Console . WriteLine ( "num1 is greater than or equal to num2" ) ;
93
- }
94
- else
95
- {
96
- // This clause is selected, but num1 is not less than num2.
97
- Console . WriteLine ( "num1 >= num2 returned false (but num1 < num2 also is false)" ) ;
98
- }
99
-
100
- if ( num1 < num2 )
101
- {
102
- Console . WriteLine ( "num1 is less than num2" ) ;
103
- }
104
- else
105
- {
106
- // The else clause is selected again, but num1 is not greater than
107
- // or equal to num2.
108
- Console . WriteLine ( "num1 < num2 returned false (but num1 >= num2 also is false)" ) ;
109
- }
110
-
111
- if ( num1 != num2 )
112
- {
113
- // This comparison is true, num1 and num2 are not equal.
114
- Console . WriteLine ( "Finally, num1 != num2 returns true!" ) ;
115
- }
116
-
117
- // Change the value of num1, so that both num1 and num2 are null.
118
- num1 = null ;
119
- if ( num1 == num2 )
120
- {
121
- // The equality comparison returns true when both operands are null.
122
- Console . WriteLine ( "num1 == num2 returns true when the value of each is null" ) ;
123
- }
124
-
125
- /* Output:
126
- * num1 >= num2 returned false (but num1 < num2 also is false)
127
- * num1 < num2 returned false (but num1 >= num2 also is false)
128
- * Finally, num1 != num2 returns true!
129
- * num1 == num2 returns true when the value of each is null
130
- */
131
- //</Snippet11>
132
-
133
- //<Snippet12>
134
- int ? c = null ;
135
-
136
- // d = c, unless c is null, in which case d = -1.
137
- int d = c ?? - 1 ;
138
- //</Snippet12>
139
-
140
- //<Snippet13>
141
- int ? e = null ;
142
- int ? f = null ;
143
-
144
- // g = e or f, unless e and f are both null, in which case g = -1.
145
- int g = e ?? f ?? - 1 ;
146
- //</Snippet13>
147
- }
148
- }
149
-
150
8
//-------------------------------------------------------------------------
151
9
class BoxingAndUnboxing
152
10
{
@@ -252,7 +110,6 @@ static void Wrap()
252
110
}
253
111
254
112
//-------------------------------------------------------------------------
255
- //<Snippet16>
256
113
class TestBoxing
257
114
{
258
115
static void Main ( )
@@ -265,20 +122,8 @@ static void Main()
265
122
// Boxing copies the value of i into object o.
266
123
object o = i ;
267
124
//</Snippet18>
268
-
269
- // Change the value of i.
270
- i = 456 ;
271
-
272
- // The change in i doesn't affect the value stored in o.
273
- System . Console . WriteLine ( "The value-type value = {0}" , i ) ;
274
- System . Console . WriteLine ( "The object-type value = {0}" , o ) ;
275
125
}
276
126
}
277
- /* Output:
278
- The value-type value = 456
279
- The object-type value = 123
280
- */
281
- //</Snippet16>
282
127
283
128
//-------------------------------------------------------------------------
284
129
class WrapExplicitBoxing
@@ -348,21 +193,6 @@ static void Main(string[] args)
348
193
}
349
194
}
350
195
351
- public class ConvertStringExample4
352
- {
353
- static void Main ( string [ ] args )
354
- {
355
- //<Snippet29>
356
- string notNumber = "abc" ;
357
- int numVal ;
358
- bool parsed = Int32 . TryParse ( notNumber , out numVal ) ;
359
-
360
- if ( ! parsed )
361
- Console . WriteLine ( "Cannot parse '{0}' to an int." , notNumber ) ;
362
- //</Snippet29>
363
- }
364
- }
365
-
366
196
//-------------------------------------------------------------------------
367
197
public class ConvertHexadecimalStrings
368
198
{
@@ -434,13 +264,6 @@ Hexadecimal value of ! is 21
434
264
}
435
265
}
436
266
437
- class ValueTypeConversion
438
- {
439
- static void Main ( )
440
- {
441
- }
442
- }
443
-
444
267
class ConvertHexStrings2
445
268
{
446
269
static void Main ( )
@@ -487,162 +310,4 @@ static void Main()
487
310
System . Console . ReadKey ( ) ;
488
311
}
489
312
}
490
-
491
- //<snippet40>
492
- class SafeCasting
493
- {
494
- class Animal
495
- {
496
- public void Eat ( ) { Console . WriteLine ( "Eating." ) ; }
497
- public override string ToString ( )
498
- {
499
- return "I am an animal." ;
500
- }
501
- }
502
- class Mammal : Animal { }
503
- class Giraffe : Mammal { }
504
-
505
- class SuperNova { }
506
-
507
- static void Main ( )
508
- {
509
- SafeCasting app = new SafeCasting ( ) ;
510
-
511
- // Use the is operator to verify the type.
512
- // before performing a cast.
513
- Giraffe g = new Giraffe ( ) ;
514
- app . UseIsOperator ( g ) ;
515
-
516
- // Use the as operator and test for null
517
- // before referencing the variable.
518
- app . UseAsOperator ( g ) ;
519
-
520
- // Use the as operator to test
521
- // an incompatible type.
522
- SuperNova sn = new SuperNova ( ) ;
523
- app . UseAsOperator ( sn ) ;
524
-
525
- // Use the as operator with a value type.
526
- // Note the implicit conversion to int? in
527
- // the method body.
528
- int i = 5 ;
529
- app . UseAsWithNullable ( i ) ;
530
-
531
- double d = 9.78654 ;
532
- app . UseAsWithNullable ( d ) ;
533
-
534
- // Keep the console window open in debug mode.
535
- System . Console . WriteLine ( "Press any key to exit." ) ;
536
- System . Console . ReadKey ( ) ;
537
- }
538
-
539
- void UseIsOperator ( Animal a )
540
- {
541
- if ( a is Mammal )
542
- {
543
- Mammal m = ( Mammal ) a ;
544
- m . Eat ( ) ;
545
- }
546
- }
547
-
548
- void UseAsOperator ( object o )
549
- {
550
- Mammal ? m = o as Mammal ;
551
- if ( m != null )
552
- {
553
- Console . WriteLine ( m . ToString ( ) ) ;
554
- }
555
- else
556
- {
557
- Console . WriteLine ( "{0} is not a Mammal" , o . GetType ( ) . Name ) ;
558
- }
559
- }
560
-
561
- void UseAsWithNullable ( System . ValueType val )
562
- {
563
- int ? j = val as int ? ;
564
- if ( j != null )
565
- {
566
- Console . WriteLine ( j ) ;
567
- }
568
- else
569
- {
570
- Console . WriteLine ( "Could not convert " + val . ToString ( ) ) ;
571
- }
572
- }
573
- }
574
- //</snippet40>
575
-
576
- //<snippet43>
577
- class ValueTypeDemo
578
- {
579
- static void PassByValue ( int i )
580
- {
581
-
582
- i = 5 ;
583
- Console . WriteLine ( "PassByValue: local copy = {0}" , i ) ;
584
- }
585
-
586
- static void PassByReference ( ref int i )
587
- {
588
-
589
- i = 5 ;
590
- Console . WriteLine ( "PassByReference: local copy = {0}" , i ) ;
591
- }
592
-
593
- static void Main ( )
594
- {
595
- int num = 1 ;
596
-
597
- // Pass num by value (default behavior for value types).
598
- PassByValue ( num ) ;
599
- Console . WriteLine ( "After calling PassByValue, num = {0}" , num ) ;
600
-
601
- //Pass num by reference.
602
- PassByReference ( ref num ) ;
603
- Console . WriteLine ( "After calling PassByReference, num = {0}" , num ) ;
604
-
605
- // Keep the console open in debug mode.
606
- Console . WriteLine ( "Press any key to exit." ) ;
607
- Console . ReadKey ( ) ;
608
- }
609
- }
610
- /*
611
- Output:
612
- PassByValue: local copy = 5
613
- After calling PassByValue, num = 1
614
- PassByReference: local copy = 5
615
- After calling PassByReference, num = 5
616
- */
617
- //</snippet43>
618
-
619
- //<Snippet46>
620
- class Base { }
621
- class Derived : Base { }
622
-
623
- class GetTypeInfo
624
- {
625
-
626
- static void Main ( )
627
- {
628
- Derived derived = new Derived ( ) ;
629
-
630
- // The 'is' keyword is polymorphic.
631
-
632
- Console . WriteLine ( "derived is Derived = {0}" , derived is Derived ) ;
633
- // true:
634
- Console . WriteLine ( "derived is Base = {0}" , derived is Base ) ;
635
-
636
- // GetType returns the exact run-time type.
637
- object o = derived ;
638
- Console . WriteLine ( "The run-time type of o is {0}" , o . GetType ( ) . Name ) ;
639
- }
640
- }
641
- /* Output:
642
- derived is Derived = True
643
- derived is Base = True
644
- The run-time type of o is Derived
645
- */
646
-
647
- //</Snippet46>
648
313
}
0 commit comments