corrected errors about the nullable types for C#

This commit is contained in:
Melvyn 2013-09-21 15:23:24 -04:00
parent fceaa4a7cf
commit 81c0480780

View File

@ -107,7 +107,8 @@ namespace Learning
// Char - A single 16-bit Unicode character // Char - A single 16-bit Unicode character
char fooChar = 'A'; char fooChar = 'A';
// Strings // Strings -- unlike the previous base types which are all value types,
// a string is a reference type. That is, you can set it to null
string fooString = "My string is here!"; string fooString = "My string is here!";
Console.WriteLine(fooString); Console.WriteLine(fooString);
@ -142,14 +143,21 @@ namespace Learning
const int HOURS_I_WORK_PER_WEEK = 9001; const int HOURS_I_WORK_PER_WEEK = 9001;
// Nullable types // Nullable types
// any type can be made nullable by suffixing a ? // any value type (i.e. not a class) can be made nullable by suffixing a ?
// <type>? <var name> = <value> // <type>? <var name> = <value>
int? nullable = null; int? nullable = null;
Console.WriteLine("Nullable variable: " + nullable); Console.WriteLine("Nullable variable: " + nullable);
// In order to use nullable's value, you have to use Value property or to explicitly cast it // In order to use nullable's value, you have to use Value property
string? nullableString = "not null"; // or to explicitly cast it
Console.WriteLine("Nullable value is: " + nullableString.Value + " or: " + (string) nullableString ); DateTime? nullableDate = null;
// The previous line would not have compiled without the '?'
// because DateTime is a value type
// <type>? is equivalent to writing Nullable<type>
Nullable<DateTime> otherNullableDate = nullableDate;
nullableDate = DateTime.Now;
Console.WriteLine("Nullable value is: " + nullableDate.Value + " or: " + (DateTime) nullableDate );
// ?? is syntactic sugar for specifying default value // ?? is syntactic sugar for specifying default value
// in case variable is null // in case variable is null