C# Basics

General

Programming Fundamentals
1. Algorithms
2. Data Structures
3. Object-oriented Programming
4. Clean coding
5. Refactoring


return a > b ? a : b; 
                                    
                                    

If a is bigger than b, return a else return b

In C# value types cannot be null (for example an int cannot be null)

A predicate is a delegate that points to a method that will return true if a certain condition is met.

EC Types
Exception Hadnling

You can use multiple catch blocks.
Always order your catch block from most specific to least specific.


catch (DivideByZeroException ex) {}
catch (ArithmeticException ex) {}
catch (Exception ex) {}
                                    

You need a finally block to dispose of unmanaged resources (not managef by CLR, thus no garbage collection). Examples include:
1. File Handles
2. Database Connections
3. Network Connections
4. Graphics Handles
Any class that uses unmanaged resources is expected to implement the IDisposable intetface which consist of a method called Dispose()


streamReader.Dispose();
                                    

You can use 'throw' to throw your own exceptions.
You could create a class and throw an exception as follows:


throw new YouTubeException("Could not find video",ex);
                                        

using statement

When using the 'using' statment the compiler will call finally under the hood to dispose the StreamReader object once you are done with it.
This is the preferred way, instead of using a finally block and called the Dispose method.


using (var streamReader = new StreamReader(....))
{
    var content = streamReader.ReadToEnd();
}
                                    
Nullable Types

Nullable is a generic structure that is defined in the system namespace.
Value type can be converted to a Nullable type, but not vise versa.
If you want to assign a Nullable type to a value type use GetValueOrDefault()
DateTime variable cannot be set to null, but using Nullable it can:


Nullable<DateTime> date = null;
DateTime? date = null;  //short hand for Nullable
//3 members of a Nullable type is:
date.GetValueOrDefault //will return the actual value if object had been initialize or will return the default value for the datetype
date.HasValue //returns true if object has a value, otherwise false
date.Value // before accessing the Value property, make sure your object has a value, the preferred way to use is GetValueOrDefault()
                                    
                                    
Null Coalescing Operator

DateTime? date = null;
DateTime date2 = date ?? DateTime.Today; //so if date has a value assign it to date2 else use DateTime.Today as value
DateTime date3 = (date != null) ? date.GetValueOrDefault() : DateTime.Today; //another way
                                    
Dynamics

DLR (Dynamic Language Runtime) sits on top of CLR (Common Language Runtime) and gives dynamic language capabilities and enables the use of dynamic
With dynamic you can call methods that is not available at CLR, but will be with DLR


//this will compile and actually convert name from a dynamic string object to a dynamic int object
dynamic name = "Douw";
name = 10;