Merge pull request #245 from golergka/master

Minor additions to C#
This commit is contained in:
Adam Bard 2013-08-17 09:27:23 -07:00
commit 1eec8e47bc

View File

@ -144,6 +144,10 @@ namespace Learning
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
string? nullableString = "not null";
Console.WriteLine("Nullable value is: " + nullableString.Value + " or: " + (string) nullableString );
// ?? is syntactic sugar for specifying default value // ?? is syntactic sugar for specifying default value
// in case variable is null // in case variable is null
int notNullable = nullable ?? 0; int notNullable = nullable ?? 0;
@ -418,10 +422,10 @@ namespace Learning
public Bicycle(int startCadence, int startSpeed, int startGear, public Bicycle(int startCadence, int startSpeed, int startGear,
string name, bool hasCardsInSpokes) string name, bool hasCardsInSpokes)
{ {
this.gear = startGear; this.gear = startGear; // "this" keyword denotes the current object
this.cadence = startCadence; this.cadence = startCadence;
this._speed = startSpeed; this._speed = startSpeed;
this.name = name; this.name = name; // it can be useful when there's a name conflict
this.hasCardsInSpokes = hasCardsInSpokes; this.hasCardsInSpokes = hasCardsInSpokes;
} }
@ -470,19 +474,19 @@ namespace Learning
// when only data needs to be accessed, consider using properties. // when only data needs to be accessed, consider using properties.
// properties may have either get or set, or both // properties may have either get or set, or both
private bool _hasTassles; // private variable private bool _hasTassles; // private variable
public bool hasTassles // public accessor public bool HasTassles // public accessor
{ {
get { return _hasTassles; } get { return _hasTassles; }
set { _hasTassles = value; } set { _hasTassles = value; }
} }
private int _frameSize; // Properties can be auto-implemented
public int FrameSize public int FrameSize
{ {
get { return _frameSize; } get;
// you are able to specify access modifiers for either get or set // you are able to specify access modifiers for either get or set
// this means only Bicycle class can call set on Framesize // 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. //Method to display the attribute values of this Object.