end of the course
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
# Classes
|
# OOP
|
||||||
|
|
||||||
|
# classes
|
||||||
|
|
||||||
In another file
|
In another file
|
||||||
```C#
|
```C#
|
||||||
@@ -90,6 +92,8 @@ class Human
|
|||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
// in the main class
|
// in the main class
|
||||||
|
|
||||||
Human human1 = new Human("Rick",65)
|
Human human1 = new Human("Rick",65)
|
||||||
@@ -287,6 +291,7 @@ Class Car
|
|||||||
```
|
```
|
||||||
|
|
||||||
```C#
|
```C#
|
||||||
|
// main
|
||||||
Car[] garage = new Car[3]
|
Car[] garage = new Car[3]
|
||||||
|
|
||||||
Car car1 = new Car("Mustang");
|
Car car1 = new Car("Mustang");
|
||||||
@@ -307,3 +312,380 @@ forach(Car car in garage)
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Object as arguments
|
||||||
|
|
||||||
|
|
||||||
|
```C#
|
||||||
|
Class Car
|
||||||
|
{
|
||||||
|
public String model;
|
||||||
|
public String color;
|
||||||
|
|
||||||
|
public Car(String model, String color)
|
||||||
|
{
|
||||||
|
this.model = model;
|
||||||
|
this.color = color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
// main
|
||||||
|
|
||||||
|
Car car1 = new car("Mustang", "red");
|
||||||
|
Car car 2 = Copy(car1)
|
||||||
|
|
||||||
|
ChangeColor(car1, "siver");
|
||||||
|
|
||||||
|
Console.WriteLine(car1.color + " " + car1.model)
|
||||||
|
|
||||||
|
Console.WriteLine(car2.color + " " + car2.model)
|
||||||
|
|
||||||
|
public static void ChangeColor(Car car, String color)
|
||||||
|
{
|
||||||
|
car.color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Car Copy(Car car)
|
||||||
|
{
|
||||||
|
return new Car(car.model, car.color);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## method overiding
|
||||||
|
|
||||||
|
provides a new version of a method inherited from a parent class
|
||||||
|
inherited method must be: abstract, virtual, or already overriden
|
||||||
|
Used with Tostring(), polymporphism
|
||||||
|
|
||||||
|
```C#
|
||||||
|
Class Animal
|
||||||
|
{
|
||||||
|
public virtual void Speak()
|
||||||
|
{
|
||||||
|
Console.WriteLine("The animal goes *brrr*")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Class Dog : Animal
|
||||||
|
{
|
||||||
|
public override void Speak()
|
||||||
|
{
|
||||||
|
Console.WriteLine("The dog goes *Woof*")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Class Cat : Animal
|
||||||
|
{
|
||||||
|
public override void Speak()
|
||||||
|
{
|
||||||
|
Console.WriteLine("The cat goes *Meow*")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
Dog dog = new Dog();
|
||||||
|
Cat cat = new Cat();
|
||||||
|
|
||||||
|
dog.Speak();
|
||||||
|
cat.Speak();
|
||||||
|
```
|
||||||
|
|
||||||
|
## ToString
|
||||||
|
|
||||||
|
Converts an object ot its string representation so that it is suitable for display
|
||||||
|
|
||||||
|
```C#
|
||||||
|
class Car
|
||||||
|
{
|
||||||
|
String make;
|
||||||
|
String model;
|
||||||
|
int year;
|
||||||
|
String color;
|
||||||
|
|
||||||
|
public Car(String make,String model,int year,String color)
|
||||||
|
{
|
||||||
|
this make = make;
|
||||||
|
this model = model;
|
||||||
|
this year = year;
|
||||||
|
this color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public overide ToString()
|
||||||
|
{
|
||||||
|
String message = "this is a " + make + " " + model;
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
// Main
|
||||||
|
|
||||||
|
Car car = new Car("Chevy", "Corvette", 2022, "blue");
|
||||||
|
|
||||||
|
Console.WriteLine(car);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Polymporphism
|
||||||
|
|
||||||
|
Objects can e identified by more than one type
|
||||||
|
Ex. A Dog is also: Canine, Animal, Organisms
|
||||||
|
|
||||||
|
```C#
|
||||||
|
class Vehicle
|
||||||
|
{
|
||||||
|
public int speed = 0
|
||||||
|
|
||||||
|
// have to add virtual for polymporphism
|
||||||
|
public virtual void Go()
|
||||||
|
{
|
||||||
|
Console.WriteLine("this vehicle is moving !");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Class Car : Vehicle
|
||||||
|
{
|
||||||
|
public int wheels = 4;
|
||||||
|
public overide void Go()
|
||||||
|
{
|
||||||
|
Console.WriteLine("this car is moving !");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Class Bicycle : Vehicle
|
||||||
|
{
|
||||||
|
public int wheels = 2;
|
||||||
|
public overide void Go()
|
||||||
|
{
|
||||||
|
Console.WriteLine("this go is moving !");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Class Boat : Vehicle
|
||||||
|
{
|
||||||
|
public int wheels = 0;
|
||||||
|
public overide void Go()
|
||||||
|
{
|
||||||
|
Console.WriteLine("this boat is moving !");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
// Main
|
||||||
|
Car car = new Car();
|
||||||
|
Bicycle bicycle = new Bicycle();
|
||||||
|
Boat boat = new Boat();
|
||||||
|
|
||||||
|
Vehicle[] vehicles = {car, bicycle, boat};
|
||||||
|
|
||||||
|
forach (vehicle vehicle in vehicle)
|
||||||
|
{
|
||||||
|
vehicle.Go();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## interfaces
|
||||||
|
|
||||||
|
defines a "contract" that all the classes inheriting from should follow
|
||||||
|
An inerface declares "what a class should have"
|
||||||
|
An inheriting class defines "how it should do it"
|
||||||
|
Benefit are security + multiple iheritance + "plug-and-play"
|
||||||
|
|
||||||
|
|
||||||
|
```C#
|
||||||
|
interface IPrey
|
||||||
|
{
|
||||||
|
void Flee();
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IPredator
|
||||||
|
{
|
||||||
|
void Hunt();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Rabbit : IPrey
|
||||||
|
{
|
||||||
|
pulic void Flee()
|
||||||
|
{
|
||||||
|
Console.WriteLine("The rabbit run away!");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class hawk : IPredator
|
||||||
|
{
|
||||||
|
pulic void Hunt()
|
||||||
|
{
|
||||||
|
Console.WriteLine("The hawk is searching for food!");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class fish : IPrey, IPredator
|
||||||
|
{
|
||||||
|
|
||||||
|
pulic void Flee()
|
||||||
|
{
|
||||||
|
Console.WriteLine("The fish swims away!");
|
||||||
|
|
||||||
|
}
|
||||||
|
pulic void Hunt()
|
||||||
|
{
|
||||||
|
Console.WriteLine("The fish is searching for food!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
```C#
|
||||||
|
// Main
|
||||||
|
|
||||||
|
Rabbit rabbit = new Rabbit();
|
||||||
|
Hawk hawk = new Hawk();
|
||||||
|
Fish fish = new Fish();
|
||||||
|
|
||||||
|
rabit.flee();
|
||||||
|
hawk.hunt();
|
||||||
|
fish.flee();
|
||||||
|
fish.hunt();
|
||||||
|
```
|
||||||
|
|
||||||
|
## getter and setter
|
||||||
|
|
||||||
|
getter & setters add security to fields by encapsulation
|
||||||
|
They're accessors found within properties
|
||||||
|
|
||||||
|
properties = combine aspects of both fields and methods (share name with a field)
|
||||||
|
get accessor = used to return the property value
|
||||||
|
set accessor = used to assign a new value
|
||||||
|
value keyword = defines the value being assigned by the set (parameter)
|
||||||
|
|
||||||
|
|
||||||
|
```C#
|
||||||
|
Class Car
|
||||||
|
{
|
||||||
|
private int speed;
|
||||||
|
|
||||||
|
public Car(int speed)
|
||||||
|
{
|
||||||
|
Speed = speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// properties
|
||||||
|
public int Speed
|
||||||
|
{
|
||||||
|
|
||||||
|
get { return speed}; //read
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if value(value > 500)
|
||||||
|
{
|
||||||
|
speed = 500;
|
||||||
|
}else{
|
||||||
|
speed = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
// Main
|
||||||
|
|
||||||
|
Car car = new Car();
|
||||||
|
|
||||||
|
Car Speed = 10000000000;
|
||||||
|
|
||||||
|
Console.WriteLine(car.Speed);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Auto-Implemted properties
|
||||||
|
|
||||||
|
shortcut when no additional logic is required in the property
|
||||||
|
you do not have to define a field for a property,
|
||||||
|
you only have to write get; and/or set; insid the property;
|
||||||
|
|
||||||
|
```C#
|
||||||
|
Class Car
|
||||||
|
{
|
||||||
|
// shortcut
|
||||||
|
public String Model {get; set;}
|
||||||
|
public Car(String model)
|
||||||
|
{
|
||||||
|
this.Model = model;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Enums
|
||||||
|
|
||||||
|
Special "class" that contains a set of named integer constans.
|
||||||
|
use enums when you have values that you know will not change,
|
||||||
|
To get the integer value from an item, you must explicitly convert to an int
|
||||||
|
|
||||||
|
name = integer
|
||||||
|
|
||||||
|
```C#
|
||||||
|
enum Planets
|
||||||
|
{
|
||||||
|
Mecury,
|
||||||
|
Venus,
|
||||||
|
Earth,
|
||||||
|
Mars,
|
||||||
|
Jupiter,
|
||||||
|
Saturn,
|
||||||
|
Uranus,
|
||||||
|
Neptune,
|
||||||
|
Pluto
|
||||||
|
}
|
||||||
|
|
||||||
|
enum PlanetsRadius
|
||||||
|
{
|
||||||
|
Mecury = 2439,
|
||||||
|
Venus = 6051,
|
||||||
|
Earth = 6371,
|
||||||
|
Mars = 3389,
|
||||||
|
Jupiter = 69911,
|
||||||
|
Saturn = 58232,
|
||||||
|
Uranus = 25362,
|
||||||
|
Neptune = 24622,
|
||||||
|
Pluto = 1188
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Console.WriteLine(Planets.Pluto + " is a planet")
|
||||||
|
|
||||||
|
|
||||||
|
String name = PlanetsRadius.Earth.ToString();
|
||||||
|
// return : Earth
|
||||||
|
|
||||||
|
int radius = (int)PlanetsRadius.Earth;
|
||||||
|
// return : 6371
|
||||||
|
```
|
||||||
|
|
||||||
|
## generics
|
||||||
|
|
||||||
|
not specific to a particular data type
|
||||||
|
add <T> to : classes, methods, fields, etc.
|
||||||
|
or other name ex : <Things>
|
||||||
|
allow for code reusability for different data types
|
||||||
|
|
||||||
|
```C#
|
||||||
|
int[] intArray = { 1, 2, 3};
|
||||||
|
double[] doubleArray = { 1.0, 2.0, 3.0};
|
||||||
|
String[] stringArray = {"1","2","3"};
|
||||||
|
|
||||||
|
// don't work for other array
|
||||||
|
public static void() displayElements<T>(T[] array)
|
||||||
|
{
|
||||||
|
foreach(T item in array)
|
||||||
|
{
|
||||||
|
Console.Write(item + " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
101
C# Full Course for free/15.List.md
Normal file
101
C# Full Course for free/15.List.md
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
# List
|
||||||
|
|
||||||
|
data structure that represents a list of objects that can be
|
||||||
|
accessed by index.
|
||||||
|
Similar to array, but can dynamically increase/decrease in size
|
||||||
|
using System.Collection.Generic
|
||||||
|
|
||||||
|
```C#
|
||||||
|
// error with array
|
||||||
|
String[] food = new string[3];
|
||||||
|
|
||||||
|
food[0] = "pizza";
|
||||||
|
food[1] = "hamburger";
|
||||||
|
food[2] = "hotdog";
|
||||||
|
food[3] = "Salad";
|
||||||
|
|
||||||
|
foreach(String item in food)
|
||||||
|
{
|
||||||
|
Console.WriteLine(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
using System.Collection.Generic
|
||||||
|
|
||||||
|
List<String> food = new List<String>();
|
||||||
|
|
||||||
|
food.Add("fries");
|
||||||
|
food.Add("pizza");
|
||||||
|
food.Add("hamburger");
|
||||||
|
food.Add("hotdog");
|
||||||
|
food.Add("Salad");
|
||||||
|
food.Add("fries");
|
||||||
|
|
||||||
|
foreach(String item in food)
|
||||||
|
{
|
||||||
|
Console.WriteLine(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
// accessing an element
|
||||||
|
Console.WriteLine(food[1]);
|
||||||
|
|
||||||
|
food.Remove("fries");
|
||||||
|
|
||||||
|
food.Insert(0,"sushie");
|
||||||
|
|
||||||
|
Console.WriteLine(food.Count());
|
||||||
|
|
||||||
|
Console.WriteLine(food.IndexOf("hotdog"));
|
||||||
|
Console.WriteLine(food.LastIndexOf("fries"));
|
||||||
|
Console.WriteLine(food.Cotains("pizza"));
|
||||||
|
food.Sort();
|
||||||
|
food.Reverse();
|
||||||
|
food.Clear();
|
||||||
|
String[] foodArray = food.ToArray();
|
||||||
|
food.Clear();
|
||||||
|
```
|
||||||
|
|
||||||
|
## List of object
|
||||||
|
|
||||||
|
```C#
|
||||||
|
List<Player> player = new List<Player>();
|
||||||
|
|
||||||
|
Player player1 = new Player ("Chad")
|
||||||
|
Player player2 = new Player ("Steve")
|
||||||
|
Player player3 = new Player ("Karen")
|
||||||
|
|
||||||
|
|
||||||
|
players.Add(player1);
|
||||||
|
players.Add(player2);
|
||||||
|
players.Add(player3);
|
||||||
|
|
||||||
|
// or anonymously
|
||||||
|
|
||||||
|
players.Add(new Player ("Karen"));
|
||||||
|
players.Add(new Player ("Chad"));
|
||||||
|
players.Add(new Player ("Steve"));
|
||||||
|
|
||||||
|
foreach(Player player in players)
|
||||||
|
{
|
||||||
|
Console.WriteLine(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
class Player
|
||||||
|
{
|
||||||
|
public string username;
|
||||||
|
|
||||||
|
public Player(String username)
|
||||||
|
{
|
||||||
|
this.username=username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return username;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
90
C# Full Course for free/16.multithreading.md
Normal file
90
C# Full Course for free/16.multithreading.md
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
# multithreading
|
||||||
|
|
||||||
|
thread is an execution path of a program
|
||||||
|
we can use multiple threads to perform,
|
||||||
|
different taks of our program at the same time.
|
||||||
|
Current thread running is "main" thread
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
## Single Thread
|
||||||
|
```C#
|
||||||
|
// single Thread
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
Thread mainThread = Thread.CurrenThread;
|
||||||
|
mainThread.Name = "Main Thread";
|
||||||
|
Console.WriteLine(mainThread.Name);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Timer Example
|
||||||
|
|
||||||
|
```C#
|
||||||
|
public static void CountDown()
|
||||||
|
{
|
||||||
|
for (int i = 10; i >=0; i--)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Timer #1 : " + i + " seconds");
|
||||||
|
Thread.Sleep(1000)
|
||||||
|
}
|
||||||
|
Console.WriteLine("Timer #1 is complete !")
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void CountUp()
|
||||||
|
{
|
||||||
|
for (int i = 0; i >=0; i++)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Timer #2 : " + i + " seconds");
|
||||||
|
Thread.Sleep(1000)
|
||||||
|
}
|
||||||
|
Console.WriteLine("Timer #2 is complete !")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
// Main
|
||||||
|
|
||||||
|
CountDown();
|
||||||
|
CountUp();
|
||||||
|
```
|
||||||
|
|
||||||
|
## multiple Thread
|
||||||
|
|
||||||
|
```C#
|
||||||
|
public static void CountDown()
|
||||||
|
{
|
||||||
|
for (int i = 10; i >=0; i--)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Timer #1 : " + i + " seconds");
|
||||||
|
Thread.Sleep(1000)
|
||||||
|
}
|
||||||
|
Console.WriteLine("Timer #1 is complete !")
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void CountUp()
|
||||||
|
{
|
||||||
|
for (int i = 0; i >=0; i++)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Timer #2 : " + i + " seconds");
|
||||||
|
Thread.Sleep(1000)
|
||||||
|
}
|
||||||
|
Console.WriteLine("Timer #2 is complete !")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
// Main
|
||||||
|
|
||||||
|
Thread thread1 = new Thread(CountDown);
|
||||||
|
Thread thread2 = new Thread(CountUp);
|
||||||
|
|
||||||
|
// if the function got a parameter
|
||||||
|
// use a lambda
|
||||||
|
// Thread thread1 = new Thread(() => CountDown("Thimer #1"));
|
||||||
|
|
||||||
|
|
||||||
|
thread1.start();
|
||||||
|
thread2.start();
|
||||||
|
|
||||||
|
CountDown();
|
||||||
|
CountUp();
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user