From 2173dd419a1daa06efee295e3fe2cdbc40224433 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 15:57:52 +0200 Subject: Accessing nullable's value in C# --- csharp.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index c254b5a9..c9df6db9 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -144,6 +144,10 @@ namespace Learning 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 ); + // ?? is syntactic sugar for specifying default value // in case variable is null int notNullable = nullable ?? 0; -- cgit v1.2.3 From 541fd3fb06bf6e97974123934ad89cb07a7ef289 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 16:01:08 +0200 Subject: C# Coding style: property names start with capitals --- csharp.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index c9df6db9..490c34bb 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -474,7 +474,7 @@ namespace Learning // when only data needs to be accessed, consider using properties. // properties may have either get or set, or both private bool _hasTassles; // private variable - public bool hasTassles // public accessor + public bool HasTassles // public accessor { get { return _hasTassles; } set { _hasTassles = value; } -- cgit v1.2.3 From 4295e85d6097e2d331ff75a958a3a5a064795410 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 16:03:35 +0200 Subject: C#: Auto-implemented properties --- csharp.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 490c34bb..951958b6 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -480,13 +480,13 @@ namespace Learning set { _hasTassles = value; } } - private int _frameSize; + // Properties can be auto-implemented public int FrameSize { - get { return _frameSize; } + get; // you are able to specify access modifiers for either get or set // this means only Bicycle class can call set on Framesize - private set { _frameSize = value; } + private set; } //Method to display the attribute values of this Object. -- cgit v1.2.3 From c3df13db567b82fc0802e46c23e4e4486618b207 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 16:06:04 +0200 Subject: C#: comments about "this" keyword --- csharp.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 951958b6..0cef8f05 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -422,10 +422,10 @@ namespace Learning public Bicycle(int startCadence, int startSpeed, int startGear, string name, bool hasCardsInSpokes) { - this.gear = startGear; + this.gear = startGear; // "this" keyword denotes the current object this.cadence = startCadence; this._speed = startSpeed; - this.name = name; + this.name = name; // it can be useful when there's a name conflict this.hasCardsInSpokes = hasCardsInSpokes; } -- cgit v1.2.3