C# Various Bits and Pieces 👷🏼♂️👷🏼♀️
Other operators
Int? myInt = null; // Int defined with a ? - object may be nullable
Int? myInt = MyPossiblyNullObject?.GetInt(); // elvis operator ?. returns null if object is null
Current Directory
using System.IO;
string current_dir = Directory.GetCurrentDirectory();
Reading from the Console
string theInput;
do {
Console.WriteLine("Prompt: ");
theInput = Console.ReadLine();
if(!theInput.Equals("exit")) {
// process theInput
}
} while (!theInput.Equals("exit"));
Preprocessor
#define DEBUG
#undef DEBUG
#if DEBUG
#else
#elif
#endif
#region ...
#endregion
Namespaces
- similiar to Java packages
- can be nested
Dates and Times
System.DateTime currentTime = System.DateTime.Now;
8 & 16 Bit Arithmetic
arithmetic is handled by 32 bit & above, anything less than 16 bits must be cast back, e.g.
short x= 1, y = 2;
short z= x + y; // compile error
short z= (short) (x + y); // runs
Special Floating Point Values
NaN (Not a number) double.NaN, float.NaN double.PositiveInfinity, float.PositiveInfinity double.NegativeInfinity, float.NegativeInfinity .MaxValue .MinValue .Epsilon (and others...)
Testing for NaN
if (x.IsNaN) ...
Garbage Collection
Handled by the garbage collection - cannot explicitly delete objects. Also, the Heap also contains statics and constants.
Assignments
local variables - undefined until assignment member variables - set to default value (0 or null) can set a variable to its default value, e.g.
decimal d = default(decimal)