Index

Notes
C# 101

Part 1: What is C#? 

Intro

Part 2: Hello World

Part 3 : The Basics of Strings

Part 4 : Searching Strings 

See at Microsoft docs

string name = "     Antoine Fricker   ";
name = name.Trim(); // Trim() + TrimEnd() + TrimStart()

Console.WriteLine($"Hello {name}."); // string interpolation

Console.WriteLine($"The name {name} has {name.Length} letters.");

name = name.Replace("Fricker", "Le plein de thunes");
Console.WriteLine($"Now you are {name}.");

string upper = name.ToUpper();
Console.WriteLine($"To upper case: {upper}");

string lower = name.ToLower();
Console.WriteLine($"To lower case: {lower}");

string lyrics = "Hello darkness my good friend";

Console.WriteLine($"Lyrics are \"{lyrics}\".");
Console.WriteLine("Contains darkness: " + lyrics.Contains("darkness").ToString());
Console.WriteLine("Contains lightness: " + lyrics.Contains("lightness").ToString());
Console.WriteLine("Starts with \"Hello\": " + lyrics.StartsWith("Hello").ToString());
Console.WriteLine("Starts with \"hello\": " + lyrics.StartsWith("hello").ToString());
Console.WriteLine("Ends with \"friend\": " + lyrics.EndsWith("friend").ToString());

Output:

Hello Antoine Fricker.
The name Antoine Fricker has 15 letters.
Now you are Antoine Le plein de thunes.
To upper case: ANTOINE LE PLEIN DE THUNES
To lower case: antoine le plein de thunes
Lyrics are "Hello darkness my good friend".
Contains darkness: True
Contains lightness: False
Starts with "Hello": True
Starts with "hello": False
Ends with "friend": True

Part 5 : Numbers and Integer Math

Part 6: Numbers and Integer Precision

Part 7: Numbers and Decimals

See Manipulate integral and floating point numbers in C#

Integral numbers.
Double precision number double have twice the number of binary digits as single-precision.
Single precision numbers are declared using the float keyword.
Decimal precision number decimal have smaller range but greater precision.

Description Min / Max
intIntegers-2147483648
2147483647
floatFloating point number-3.402823E+38
3.402823E+38
doubleFloating point number
Doubles the number of binary digits used to endcode number
-1.79769313486232E+308
1.79769313486232E+308
decimalFloating point number
Smaller range but greater precision than double
-79228162514264337593543950335
79228162514264337593543950335
etc.
Console.WriteLine("Integers");

int a = 18; 
int b = 12;
int c = 2;

int sum = a + b;
Console.WriteLine($"{a.ToString()} + {b.ToString()} = {sum.ToString()}");

int division = a / b;
Console.WriteLine($"{a.ToString()} / {b.ToString()} = {division.ToString()}");

int priority = a + b * c;
Console.WriteLine($"{a.ToString()} + {b.ToString()} * {c.ToString()} = {priority.ToString()}");

int max = int.MaxValue; // num > max: overflow
int min = int.MinValue; // num < min: underflow
Console.WriteLine($"The range of integers is {min} to {max}");

int overflow = max + 3;
Console.WriteLine($"An example of overflow: {overflow}");

Console.WriteLine("\n");
Console.WriteLine("Float & Doubles");

double double_a = 22;
double double_b = 4;

Console.WriteLine($"{double_a.ToString()} / {double_b.ToString()} = {(double_a / double_b).ToString()}");

double_a = 10.4;
Console.WriteLine($"{double_a.ToString()} / {double_b.ToString()} = {(double_a / double_b).ToString()}");

double double_min = double.MinValue;
double double_max = double.MaxValue;
Console.WriteLine($"The range of the double type is {double_min} to {double_max}");

double double_third = 1.0 / 3.0;
Console.WriteLine($"One third: {double_third}");


Console.WriteLine("\n");
Console.WriteLine("Decimal");

decimal decimal_min = decimal.MinValue;
decimal decimal_max = decimal.MaxValue;
Console.WriteLine($"The range of the decimal type is {decimal_min} to {decimal_max}");

decimal decimal_c = 1.0M;
decimal decimal_d = 3.0M;
Console.WriteLine(decimal_c / decimal_d);

Console.WriteLine("\n");
Console.WriteLine("Exercice");

double radius = 2.5;
double area = radius * radius * Math.PI;
Console.WriteLine($"Area of a circle whose {radius} is 2.50cm: {area}cm².");

Output:

Integers
18 + 12 = 30
18 / 12 = 1
18 + 12 * 2 = 42
The range of integers is -2147483648 to 2147483647
An example of overflow: -2147483646


Float & Doubles
22 / 4 = 5.5
10.4 / 4 = 2.6
The range of the double type is -1.79769313486232E+308 to 1.79769313486232E+308
One third: 0.333333333333333


Decimal
The range of the decimal type is -79228162514264337593543950335 to 79228162514264337593543950335
0.3333333333333333333333333333


Exercice
Area of a circle whose 2.5 is 2.50cm: 19.6349540849362

Part 8: Branches (if)

Part 9: « Hello World » Explained

Part 10: What are loops?

Part 11: Combining Branches and Loops

Part 12: Arrays, List, and Collections

Part 13: Sort, Search, and Index Lists

Part 14: Lists of Other Types

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var names = new List<string> { "John", "Anna", "Lisa" };

            names.Add("Homer");
            names.Remove("Lisa");

            foreach (string name in names)
            {
                Console.WriteLine($"Hi {name.ToUpper()}");
            }

            Console.WriteLine($"The last name of the list is: {names[names.Count - 1].ToUpper()}");



            string searchedName = "Peter";
            Console.WriteLine($"Found {searchedName} at: {names.IndexOf(searchedName)}");

            searchedName = "John";
            Console.WriteLine($"Found {searchedName} at: {names.IndexOf(searchedName)}");

            names.Sort();
            Console.WriteLine($"After sort, found {searchedName} at: {names.IndexOf(searchedName)}");
        }
    }
}
using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> fibonacciNumbers = new List<int> { 1, 1 };

            while(fibonacciNumbers.Count < 30)
            {
                int a = fibonacciNumbers[fibonacciNumbers.Count - 1];
                int b = fibonacciNumbers[fibonacciNumbers.Count - 2];
                fibonacciNumbers.Add(a + b);
            }

            foreach(int fibonacciNumber in fibonacciNumbers)
            {
                Console.WriteLine(fibonacciNumber.ToString());
            }
        }
    }
}

Part 15: Debugging

Part 16: Object Oriented Programming

Part 17: OOP – Methods and Members

Part 18: OOP – Methods and Exceptions

Part 19: OOP – Catching Exceptions

using System;
using System.Collections.Generic;
using System.Text;

namespace FrickerBank
{
    class Program
    {
        static void Main(string[] args)
        {
            BankAccount account1;
            BankAccount account2;

            
            account1 = new BankAccount("Antoine FRICKER", 65000.00M);
            try {
                account1.MakeDeposit(2500.00M, DateTime.Now, "Bill  #2103_000120");
                account1.MakeDeposit(1200.00M, DateTime.Now, "Bill #2103_000124");
                account1.MakeWithdrawal(4200.00M, DateTime.Now, "Dell computer");
                account1.MakeDeposit(10000.00M, DateTime.Now, "Sold Subaru");
                account1.MakeDeposit(10.30M, DateTime.Now, "Bread");
            }
            catch(ArgumentOutOfRangeException e)
            {
                Console.WriteLine("ArgumentOutOfRangeException: " + e.ToString());
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine("InvalidOperationException: " + e.ToString());
            }
            finally
            {
                Console.WriteLine("");
                Console.WriteLine(account1.GetHistory());
            }

            Console.WriteLine("\n\n");

            account2 = new BankAccount("Julia DUONG", 1265000.00M);
            try
            {
                account2.MakeWithdrawal(88200.00M, DateTime.Now, "Red Porsche Cayenne");
                account2.MakeWithdrawal(120800.00M, DateTime.Now, "Yellow Porsche Cayenne");
                account2.MakeWithdrawal(3200800.00M, DateTime.Now, "Cannes mansion");
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine("ArgumentOutOfRangeException: " + e.ToString());
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine("InvalidOperationException: " + e.ToString());
            }
            finally
            {
                Console.WriteLine("");
                Console.WriteLine(account2.GetHistory());
            }
        }
    }

    class BankAccount
    {
        private static int ACCOUND_SEED = 0;
        public string Number { get; }
        public string Owner { get; }
        public decimal Balance { get; set; }
        public List<Transaction> Transactions { get; }

        public BankAccount(string owner, decimal initialBalance)
        {
            this.Number = (++BankAccount.ACCOUND_SEED).ToString();
            this.Owner = owner;
            this.Transactions = new List<Transaction> { };
            this.Balance = 0;
            
            
            Console.WriteLine($"Account created for {this.Owner}");

            this.MakeDeposit(initialBalance, null, "Initial desposit");
        }

        public void MakeDeposit(decimal movement, DateTime? date = null, string note = null)
        {
            if (movement < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(movement), $"Amount of deposit must be positive");
            }
            this.Balance += movement;
            this.Transactions.Add(new Transaction(movement, date, note));
        }

        public void MakeWithdrawal(decimal movement, DateTime? date = null, string note = null)
        {
            if (movement < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(movement), $"Amount of withdrawal must be positive");
            }
            else if (this.Balance - movement < 0)
            {
                throw new InvalidOperationException("Not sufficient fund for this withdrawal");
            }
            this.Balance -= movement;
            this.Transactions.Add(new Transaction(-movement, date, note));
        }

        public string GetHistory()
        {
            var report = new StringBuilder();
            report.AppendLine("Date\t\t\t\t\tAmount\tNote");
            foreach(Transaction transation in Transactions)
            {
                report.AppendLine(transation.GetLine());
            }
            return report.ToString();
        }
    }

    class Transaction
    {
        public DateTime Date { get; }
        public decimal Movement { get; }
        public string Note { get; }

        public Transaction(decimal movement, DateTime? date = null, string note = null)
        {
            this.Movement = movement;
            this.Date = (date == null) ? DateTime.Now : (DateTime)date;
            this.Note = (note == null) ? "" : note;
            Console.WriteLine(this.GetLine());
        }

        public string GetLine()
        {
            string movementStr = this.Movement.ToString();
            if (this.Movement >= 0)
            {
                movementStr = "+" + this.Movement;
            }
            return $"{(this.Date).ToString().PadRight(20)}\t{movementStr.PadLeft(20)}\t{this.Note}";
        }
    }
}