summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMelvyn <melvyn.laily@gmail.com>2013-09-21 15:23:24 -0400
committerMelvyn <melvyn.laily@gmail.com>2013-09-21 15:23:24 -0400
commit81c0480780a600ef27071285e002e8ea78dc727c (patch)
tree7e8f2adc9b5a09bd6827d1966313b7b5e8331b88
parentfceaa4a7cf0bcca54306ce6994f273f6b3710290 (diff)
corrected errors about the nullable types for C#
-rw-r--r--csharp.html.markdown18
1 files changed, 13 insertions, 5 deletions
diff --git a/csharp.html.markdown b/csharp.html.markdown
index bb36d6ce..16e3749e 100644
--- a/csharp.html.markdown
+++ b/csharp.html.markdown
@@ -107,7 +107,8 @@ namespace Learning
// Char - A single 16-bit Unicode character
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!";
Console.WriteLine(fooString);
@@ -142,14 +143,21 @@ namespace Learning
const int HOURS_I_WORK_PER_WEEK = 9001;
// 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>
int? nullable = null;
Console.WriteLine("Nullable variable: " + nullable);
- // In order to use nullable's value, you have to use Value property or to explicitly cast it
- string? nullableString = "not null";
- Console.WriteLine("Nullable value is: " + nullableString.Value + " or: " + (string) nullableString );
+ // In order to use nullable's value, you have to use Value property
+ // or to explicitly cast it
+ 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
// in case variable is null