Programing - astr0Nika/sledManagement GitHub Wiki

C#

  • object-oriented programming

Compiling

  1. Write code in C#
  2. C# compiler checks syntax and source code
  3. Intermediate language is generated
  4. CLR runs entry point method
  5. Intermediate language translate to native code by JIT compiler

Programmieren

Definition

Computer programming is a framework of series of instructions that a computer can run. Using any programming language a programmer can design and implement algorithms, procedures, etc.

LIFO, FIFO

Last in First out

It is a pile of something for example cloths. The last thing you put on top of the file is the first thing you have to take off to get to the other cloths.

First in First out

It's a queue for example in the cafeteria. If you got there first you be the first to be served. If you came second you'll be served second, end so on.

KISS, DRY, SOLID

Keep It Simple, Stupid

  • Don't use clever tricks when a simple loop or operation will do.
  • Small, readable methods > one big complex one.
  • break down the problem into smaller manageable parts

Don't Repeat Yourself

  • reduce repetition of information
  • more abstraction

SOLID Principles

Single responsibility: Class should only have one reason to change.

Open-closed: Ability to extend the code not modify it.

Liskov substitution: Child should be able to do everything a parent class can

Interface segregation:

Dependency Inversion: High-Level modules shouldn't depend on Low-Level modules. They should both depend on an abstraction.

Coding conventions

  • consistent and readable codebase

Example:

  • file organization
  • indentation
  • declarations
  • naming conventions

Algorithmen

Sorting algorithm

Good sorting is important for efficiency of other algorithm and functions.

Bubble sort: Compares two values. If second value is smaller, switch. Continue with every element multiple times till array is sorted.

Cocktail shaker sort: Reads array, finds the smallest value and puts it at the start. Next read array does not not contain the moved value.

Merge sort: Divide unsorted array into small sbu-lists of each value. Merge the small lists in sorted positions. Continue till done

Quicksort: chose a pivot. put all smaller values to the left and all larger values to the right. The pivot is now in the correct spot. Take the left values, chose a pivot and rearrange again. Do the same on the right

Bogosort: 1. randomize array; 2. check if sorted; 3. if sorted return else step 1

Search algorithm

Linear search: compare each element sequentially until it matches the wanted value [O(n)]

Binary search: for sorted arrays!! splits array in half, checks if value is smaller or larger than middle. If smaller split the left and if larger split the right. Repeat the process until the element is found [O(log n)]

Programming Language

Machine language

Machine code written in binary to control a CPU (Central Processing Unit). Assembly was developed to write code in text format.

High-level languages

High-level languages were created to write a simpler, more understandable code and less bound to the hardware. The amount of abstraction provided defines how "high-level" a programming language is.

Low-level language

Low-level language has little to no abstraction from the computer commands for CPU.

Object Oriented Programming

OOP is an idea of breaking the problem in objects, creating classes with attributes or properties and functions.

Examples:

  • java
  • c++
  • python
  • c#

Abstraction

Show only necessary details of the object like it's properties and functions. The user of an object doesn't need to know how a function works, they just need to use it.

A class is already a form of abstraction. Than we can also create interfaces, abstract classes, etc.

Polymorphism

Same function different doing

    abstract class Animal {
        public abstract void MakeSound();
    }

    class Dog : Animal {
        public override void MakeSound() {
            Console.WriteLine("Woof!");
        }
    }

    class Cat : Animal {
        public override void MakeSound() {
            Console.WriteLine("Miau!");
        }
    }

    class Main {
        public void Programm(){
            Animal dog = new Dog();
            Animal cat = new Cat();

            dog.MakeSound();  // Woof!
            cat.MakeSound(); // Miau!
        }

    }

Inheritance

Reusability: Creating a class and being able to extend it to other classes. Animals is the base class and Dog, Cat, Bird are Child classes inheriting from the base class.

    class Animal {
        public void Move() {
            Console.WriteLine("Animal is moving...");
        }
    }

    class Bird : Animal {
        public void Fly() {
            Console.WriteLine("Bird is flying...");
        }
    }

    Bird bird = new Bird();
    bird.Move(); // Inherited
    bird.Fly();  // Own method
Diamond Problem

If the object programming language allows multi inheritance, we will run in a problem where a class can inherit two other classes which could potentioally inherit the same class. Now the first mentioned class would have two function (from two other inherited classes) with the same naming (same inherited class). When calling one function with that name, the program wouldn't know what to run.

Multi Inheritance languages:

  • c++
  • python
  • perl

Single Inheritance language:

  • c#
  • php
  • swift
  • javascript

Encapsulation

Hiding data and it's complexity.

c# -> Access Modifiers

    class Person {
        private string _name;

        public Person(string name) {
            _name = name;
        }

        public string Name {
            get { return _name; }
            set { _name = value; }
        }
    }

Not OOP

Procedural Programming

  • focus on functions or subroutines that operate on data

Examples:

  • C

Functional Programming

  • avoids changing state and mutable data

Examples:

  • Lisp

Functional Programming

Garbage collector

Is an automatic memory manager for freeing space in memory. In languages like C the developer needs to manage memory and free it when something isn't being used. Where as C# comes with a garbage collector which takes that rolle away from the dev.

Wenn keine reference auf ein object, dann löschen!

Static vs Dynamic

???

Compiler

// TODO: tbd

It translate code from one programming language (source language) to another one (target language).

AOT (Ahead of Time) Compiler ->

JIT (Just in Time) Compiler compiles code during the execution of a program. It usually translates bytecode to machine code.

Uses compiler:

  • C
  • C++
  • Rust
  • Go

Uses JIT compiler:

  • Java (Java Virtual Machine)
  • C# (Common Language Runtime)

Interpreter

Uses interpreter:

  • lua
  • PHP
  • Python
  • JavaScript

Debugger

Debugger helps with testing. It gives the ability to run or stop the program on a specific point

Rekursive Funktion

Has a function that calls itself. The important thing is to have a break condition otherwise it will loop infinitely. Factorial (Fakultät) can be programed with a recursion

private int Factorial(int num){
    if (num == 0 || num == 1){
        return 1;
    }

    return num * Factorial(num -1);
}

public void Main() {
    int myNumber = 20;
    var result = Factorial(myNumber);
    console.WriteLine(result);
}

Recursive builds a stack with every new function call. Then as it is done with that called recursive function it builds down that stack.

Variablen, Konstanten

Deklaration -> erstellen, name vergeben

Initialisierung -> erste wert vergeben, initial value