Skip to content

Courses

The Complete C# Masterclass

/# Video Topic Projects
01.01 Welcome and a brief introduction to the Course
01.02 Guide Lecture - How to install Visual Studio
01.03 Guide lecture - Creating a project in Visual Studio
01.04 Your first C# program
02.01 What is a variable and what is its relationship with the data types IntegerDataTypes
02.02 The "numbers" data type - Integers
02.03 The "numbers with a decimal point data types - float, double, decimal FloatingPointDataTypes
02.04 The "Yes or No" data types - booleans Boolean
02.05 The "single symbol" dat atypes - characters Characters
02.06 The "information as text" data types - strings Strings
02.07 Collections of information from a specific data type - arrays Arrays
02.08 Some cool, useful tricks with strings StringTricks
02.09 Transforming any data type into a string - allows you to use string methods
02.10 The 3 different ways to build strings
02.11 The 3 different ways to convert one data type to another
03.01 Write vs WriteLine, when to use which? WriteandWriteLine
03.04 Accepting single character inputs from the Console - Read method ReadingCharacter
03.05 Accepting string inputs from the Console - ReadLine method ReadLine
03.06 Accepting inputs as keys from the Console - ReadKey ReadKey
03.07 Changing the color of the text and the background of the text in the Console ConsoleColors
03.08 Changing cursor settings in the Console - Size, Visibility, Position CursorSettings
03.09 Controlling the size of the Console window - WindowSize, BufferSize and more ConsoleSize
04.01 Arithmetic Operators
04.02 Assignment Operators
04.03 Comparison Operators
04.04 Logical Operators &&, ||
04.05 Ternary Operator
06.02 Practicing while loops MiniWhileGame
06.04 The for loops and their common uses ForLoops
06.05 Practicing for loops MenuWhile
06.06 foreach loop ForeachLoops
07.05 Methods with variable number of arguments params ListExamples
07.08 Methods with ref and out arguments ref, out RefAndOut
08.01 Introduction to one-dimensional arrays IntroductionToArrays
08.02 Outputting arrays string.Join() OutputtingArrays
08.03 Correctly cloning arrays Array.Clone() ArrayCloning
08.04 Reversing arrays Array.Reverse()
08.05 Bubble sorting algorithm BubbleSort
08.09 Lists List<T>
08.10 Practice working with List<T>s List<T> ListExamples
09.01 Introduction to multidimensional arrays
11.01 Introduction to exception handling try catch
11.02 Catching multiple exceptions
11.03 Inspecting caught exception string.Substring() MultipleExceptions
11.04 finally block finally TryCatchFinally
12.01 Introduction to object-oriented programming
12.02 Creating a basic class RPGCharGen
12.03 Fields and properties - the variables of a class RPGCharGen
12.04 Methods - the actions of a class RPGCharGen
12.05 Constructors - the builders of a class RPGCharGen
12.06 Namespaces and files - structuring your project RPGCharGen
13.02 Controlling the accessors of a property - read, write, and read-write properties RPGCharGen
13.03 Implementing validation in properties RPGCharGen
13.04 Validation and exceptions throw RPGCharGen
13.05 Properties and fields - when to use which RPGCharGen
14.01 this this RPGCharGen
14.03 Overloading constructors RPGCharGen
14.04 Chaining constructors RPGCharGen
15.01 public and private access modifiers public, private RPGCharGen
15.02 internal and protected access modifiers internal, protected RPGCharGen
16.01 Static fields and properties RPGCharGen
16.02 const and readonly
16.03 Static methods
16.04 Static classes
16.05 enum
17.01 Introduction to Inheritance

Learn C# By Building Applications

/# Video Topic Projects
02.12 Static vs. non-static

ArrayCloning

int[] primes = { 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, };
//int[] primesClone = (int[])primes.Clone();
int[] primesClone = new int[primes.Length];
Array.Copy(primes, primesClone, primes.Length);
primesClone[primesClone.Length -1 ] = 47;

foreach (var i in primesClone)
{
    Console.WriteLine(i);
}

Arrays

int[] numbers = new int[10];

for (int i = 0; i < numbers.Length; i++) {
    System.Console.WriteLine(numbers[i]);
}

string[] fruits = { "apple", "banana", "jackfruit", "kiwi", "mango" };

for (int i = 0; i < fruits.Length; i++)
{
    System.Console.WriteLine(fruits[i]);
}

Boolean

int firstNumber = 4;
int secondNumber = 6;

bool isSmaller = firstNumber < secondNumber;

bool isTheCookieJarFull = false;
bool isTheCookieJarEmpty = !isTheCookieJarFull;

Characters

System.Console.InputEncoding = System.Text.Encoding.UTF8;
System.Console.OutputEncoding = System.Text.Encoding.UTF8;

char x = 'x';
System.Console.WriteLine(x);

char plus = '\u002b';
System.Console.WriteLine(plus);

char umlaut = '\u00F6';
System.Console.WriteLine(umlaut);

ConsoleColors

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Once upon a midnight dreary");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("While I pondered weak and weary");
Console.ResetColor();
Console.WriteLine("Over many a quaint and curious volume of forgotten lore,");

ConsoleSize

Console.WindowHeight = 20;
Console.WindowWidth = 20;
Console.SetWindowSize(30, 30);

Console.BufferHeight= 40;
Console.BufferWidth = 40;

Console.WindowLeft = 10;
Console.WindowTop = 10;
Console.SetWindowPosition(10, 10);

CursorSettings

Console.Title = "Terminal";
Console.CursorVisible = true;
Console.CursorSize = 50;
Console.SetCursorPosition(20, 10);

ExceptionHandling

Console.WriteLine("'q' to quit");
List<int> primes = new List<int>();

while (true)
{
    string input = Console.ReadLine();
    try
    {
        primes.Add(Int32.Parse(input));
    }
    catch (FormatException ex)
    {
        if (input.ToLower() == "q")
        {
            break;
        }
        else
        {
            Console.WriteLine(ex.Message);
            //string stacktrace = ex.StackTrace;
            //string filename = stacktrace.Substring(stacktrace.IndexOf(':') - 1);
            //Console.WriteLine(filename);
        }
    }

}
Console.WriteLine(String.Join(" ", primes));

FloatingPointDataTypes

float floatNum = 13.14321365431f;

string output = $"Floating point numbers have a maximum precision of 8 digits: {floatNum}";
System.Console.WriteLine(output);

float radius = 3.5f;
double area = System.Math.PI * System.Math.Pow(radius, 2d);
System.Console.WriteLine($"A circle with a radius of {radius} has an are of {area}.");

float floatMax = float.MaxValue;
float floatMin = float.MinValue;
System.Console.WriteLine($"float ranges from {floatMin} to {floatMax}");

double doubleMax = double.MaxValue;
double doubleMin = double.MinValue;
System.Console.WriteLine($"double ranges from {doubleMin} to {doubleMax}");

decimal decimalMax = decimal.MaxValue;
decimal decimalMin = decimal.MinValue;
System.Console.WriteLine($"decimal ranges from {decimalMin} to {decimalMax}");

ForeachLoops

int[] primes = { 1, 2, 3, 5, 7, 11, 13, 17, 19, 23 };
foreach (var item in primes)
{
    Console.WriteLine(item);
}

ForLoops

int[] primes = { 1, 2, 3, 5, 7, 11, 13, 17, 19 };

for (int i = 0; i <primes.Length; i++)
{
    primes[i] = 2 * primes[i];
    Console.WriteLine(primes[i]);
}

IntegerDataTypes

int intMax = int.MaxValue;
int intMin = int.MinValue;

string output = $"int ranges from {intMin} to {intMax}";
Console.WriteLine(output);

uint uintMin = uint.MinValue;
uint uintMax = uint.MaxValue;

output = $"uint ranges from {uintMin} to {uintMax}";
Console.WriteLine(output);

byte byteMin = byte.MinValue;
byte byteMax = byte.MaxValue;

output = $"byte ranges from {byteMin} to {byteMax}";
Console.WriteLine(output);

long longMin = long.MinValue;
long longMax = long.MaxValue;

output = $"long ranges from {longMin} to {longMax}";
Console.WriteLine(output);

ulong ulongMin = ulong.MinValue;
ulong ulongMax = ulong.MaxValue;

output = $"ulong ranges from {ulongMin} to {ulongMax}";
Console.WriteLine(output);

ListExamples

static void Main(string[] args)
{
    int[] arr1 = { 0, 1, 2, 3, 4 };
    int[] arr2 = { 5, 6, 7, 8, 9 };

    List<int> list = new List<int>();
    list = splat(arr1, arr2);

    foreach (var el in list)
    {
        Console.WriteLine(el);
    }
}

static List<int> splat(params int[][] arg)
{
    List<int> output = new List<int>();
    foreach (int[] arr in arg)
    {
        output.AddRange(arr);
    }
    return output;
}

MiniWhileGame

static void Main(string[] args)
{
    int mage = 30;
    int warrior = 40;

    while (mage > 0 && warrior > 0)
    {
        warrior -= mage_attack();
        mage -= warrior_attack();
    }
    Console.WriteLine($"Conflict ends with Mage having {mage} points and Warrior having {warrior}!");
}

private static int warrior_attack()
{
    var r = new System.Random();
    int result = r.Next(1, 3);
    Console.WriteLine($"Warrior attacks Mage, dealing {result} damage!");
    return result;
}

private static int mage_attack()
{
    var r = new System.Random();
    int result = r.Next(7, 9);
    Console.WriteLine($"Mage attacks Warrior, dealing {result} damage!");
    return result;
}
static void Main(string[] args)
{
    string[] fruits = { "Apple", "Banana", "Date", "Grape", "Jackfruit", "Kiwi", "Lime", "Orange", "Peach", "Strawberry" };

    string[] menu = { "Add New Item", "Edit Item", "Remove Item", "View All Items", "Exit" };
    int choice;
    do
    {
        choice = Menu(menu);

        switch (choice)
        {
            case 1:
                for (int i = 0; i < fruits.Length; i++)
                {
                    if (fruits[i] == null)
                    {
                        Console.Write("Please add a new fruit: ");
                        fruits[i] = Console.ReadLine();
                        break;
                    }
                }
                break;
            case 2:
                int chosenFruit = 0;
                do
                {
                    Console.Write($"Edit fruit number (1 to {fruits.Length}): ");
                    try { chosenFruit = Convert.ToInt32(Console.ReadLine()); }
                    catch
                    {
                        InvalidInput();
                    }
                } while (chosenFruit == 0);
                Console.Write($"Enter new value for the {fruits[chosenFruit - 1]}: ");
                fruits[chosenFruit - 1] = Console.ReadLine();
                break;
            case 3:


                break;
            case 4:
                Console.WriteLine("Current fruits: ");
                for (int i = 0; i < fruits.Length; i++)
                {
                    if (fruits[i] != null)
                    {
                        Console.WriteLine(fruits[i]);
                    }
                }
                break;
            case 5:
                break;
            default:
                InvalidInput();
                break;
        }
    } while (true);
}

private static int Menu(string[] menu)
{
    Console.WriteLine('\n');
    for (var i = 0; i < menu.Length; i++)
    {
        Console.WriteLine("{0}. {1}", i + 1, menu[i]);
    }
    Console.Write("Your choice: ");
    string input = Console.ReadLine();
    try
    {
        int output = Int32.Parse(input);
        return output;
    }
    catch (FormatException)
    {
        InvalidInput();
        return -1;
    }
    catch (OverflowException)
    {
        InvalidInput();
        return -1;
    }
}

private static void InvalidInput()
{
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("Invalid input!");
    Console.ResetColor();
}

OutputtingArrays

int[] primes = { 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 };

Console.WriteLine($"{primes.Length} total primes");

List<int> lessThanTwenty = new List<int>();
List<int> moreThanTwenty = new List<int>();

foreach (int i in primes)
{
    if (i < 20) { lessThanTwenty.Add(i); }
    else        { moreThanTwenty.Add(i); }
}

Console.WriteLine($"Primes < 20:\n  {string.Join(", ", lessThanTwenty)}");
Console.WriteLine($"Primes >= 20:\n  {string.Join(", ", moreThanTwenty)}");

ReadingCharacter

Console.Write("How old are you? ");
int age;
int.TryParse(Console.ReadLine(), out age) ;
Console.WriteLine($"You are {age} years old (allegedly).");

ReadKey

Console.WriteLine($"\n\nKey pressed: {key.Key}");
Console.WriteLine($"Key as char: {key.KeyChar}");
Console.WriteLine($"Modifiers: {key.Modifiers}");

ReadLine

Console.Write("Input the drive letter: ");
string driveLetter = Console.ReadLine();
Console.Write("Input the folder path: ");
string folderPath = Console.ReadLine();
Console.Write("Input the file name: ");
string fileName = Console.ReadLine();
Console.WriteLine($"{driveLetter}:\\{folderPath}\\{fileName}.exe");

RefAndOut

static void Main(string[] args)
{
    int number = 0;
    Console.WriteLine(number);
    IncreaseByOne(ref number);
    Console.WriteLine(number);

}

static void IncreaseByOne(ref int n)
{
    n++;
}
static void Main()
{
    double n = 5;
    double nSquared;
    square(n, out nSquared);
    Console.WriteLine($"{n} ^ 2 = {nSquared}");
}
static void square(double x, out double y)
{
    y = System.Math.Pow(x, 2);
}

RPGCharGen

Strings

string username = "admin";
System.Console.WriteLine(username[0]);

// impossible, strings are immutable
username[0] = 'A';

StringTricks

string fruitJuice = "Strawberry juice";
string separator = new string('-', fruitJuice.Length);
System.Console.WriteLine(fruitJuice);
System.Console.WriteLine(separator);

System.Console.WriteLine(fruitJuice.Contains("j"));
System.Console.WriteLine(fruitJuice.IndexOf("r"));
System.Console.WriteLine(fruitJuice.LastIndexOf("r"));
System.Console.WriteLine(fruitJuice.ToUpper().Contains("J"));
System.Console.WriteLine(fruitJuice.ToUpper().IndexOf("RR"));
System.Console.WriteLine(fruitJuice.ToUpper().LastIndexOf("RR"));

TryCatchFinally

StreamWriter sw = null;
try
{

    sw = File.CreateText(Directory.GetCurrentDirectory() + @"/test.txt");

    int number = int.Parse(Console.ReadLine());
    int dividend = 5 / number;

    sw.Write(number);
}
catch (FormatException ex)
{
    Console.WriteLine(ex.Message);
}
catch (DivideByZeroException ex)
{
    Console.WriteLine(ex.Message);
}
finally
{
    sw.Close();
}

WriteAndWriteLine

string heading = "Protein Intake Week: 1";
string underline = new string('=', heading.Length);

double num1 = 80.885570;
double num2 = 94.564645;
double num3 = 78.678931;
double num4 = 88.66654;
double num5 = 88.6466;
double num6 = 76.777;
double num7 = 91.85759;
double sum = num1 + num2 + num3 + num4 + num5 + num6 + num7;
double[] array = { num1, num2, num3, num4, num5, num6, num7 };

System.Console.WriteLine("|{0}|", heading);
System.Console.WriteLine("|{0}|", underline);
foreach (double i in array)
{ 
    System.Console.WriteLine($"|{i, 22:N2}|"); 
}
System.Console.WriteLine("|{0}|", underline);
System.Console.WriteLine("|Total: {0, 15:N2}|", sum);

Test-Driven Development in C#

Create a Red Unit Test

Mocking with Moq and xUnit