pause at 3:00
This commit is contained in:
@@ -16,3 +16,36 @@ Console.WriteLine(cars.Length)
|
|||||||
|
|
||||||
String[] cars = new string[3]
|
String[] cars = new string[3]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Multidimention arrays
|
||||||
|
|
||||||
|
```C#
|
||||||
|
String[] ford = {"Mustang", "F-150", "Explorer"};
|
||||||
|
String[] chevy = {"Corvette", "Camaro", "Silverado"};
|
||||||
|
String[] toyota = {"Corolla", "Camry", "Rav4"};
|
||||||
|
|
||||||
|
String[,] parkingLot = {
|
||||||
|
{"Mustang", "F-150", "Explorer"}
|
||||||
|
{"Corvette", "Camaro", "Silverado"};
|
||||||
|
{"Corolla", "Camry", "Rav4"};
|
||||||
|
};
|
||||||
|
|
||||||
|
parkingLot[0,2] = "Fusion";
|
||||||
|
//"Explorer" to "Fusion"
|
||||||
|
|
||||||
|
parkingLot[2,0] = "Tacoma";
|
||||||
|
//"Corolla" to "Tacoma"
|
||||||
|
|
||||||
|
foreach(String car in parkingLot){
|
||||||
|
Console.WriteLine(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < parkingLot.GetLength(0); i++)
|
||||||
|
{
|
||||||
|
for(int j = 0; j < parkingLot.GetLength(1); i++)
|
||||||
|
{
|
||||||
|
Console.Write(parkingLot[i,j] + " ")
|
||||||
|
}
|
||||||
|
Console.WriteLine();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -23,3 +23,62 @@ Console.WriteLine("Happy birthday to you" + name)
|
|||||||
Console.WriteLine("you are " + age + "years old")
|
Console.WriteLine("you are " + age + "years old")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## return
|
||||||
|
|
||||||
|
```C#
|
||||||
|
double x = 5.3;
|
||||||
|
double y = 3;
|
||||||
|
|
||||||
|
double result = Multiply(x,y)
|
||||||
|
|
||||||
|
static double Multiply(double x, double y){
|
||||||
|
double z = x * y;
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## method overloading
|
||||||
|
|
||||||
|
```C#
|
||||||
|
double x = 5.3;
|
||||||
|
double y = 3;
|
||||||
|
double z = 3;
|
||||||
|
|
||||||
|
double result = Multiply(x,y)
|
||||||
|
// Will return the first Multiply method
|
||||||
|
double result = Multiply(x,y,z)
|
||||||
|
// Will return the second Multiply method
|
||||||
|
|
||||||
|
static double Multiply(double x, double y){
|
||||||
|
double z = x * y;
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double Multiply(double a, double b, double c){
|
||||||
|
double z = x * y;
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## params
|
||||||
|
|
||||||
|
params keyword a method parameter that takes a variale number of arguments.
|
||||||
|
the parameter type must be a single - dimensional array
|
||||||
|
|
||||||
|
```C#
|
||||||
|
|
||||||
|
double total = CheckOut(3.99, 5,75, 15);
|
||||||
|
|
||||||
|
static double Checkout(params double[] prices){
|
||||||
|
|
||||||
|
double total =0;
|
||||||
|
foreach(double price in prices)
|
||||||
|
{
|
||||||
|
total += prices;
|
||||||
|
}
|
||||||
|
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|||||||
309
C# Full Course for free/14.OOP.md
Normal file
309
C# Full Course for free/14.OOP.md
Normal file
@@ -0,0 +1,309 @@
|
|||||||
|
# Classes
|
||||||
|
|
||||||
|
In another file
|
||||||
|
```C#
|
||||||
|
class Messages()
|
||||||
|
{
|
||||||
|
public static void Hello()
|
||||||
|
{
|
||||||
|
Console.WriteLine("Hello! Welcome to the program");
|
||||||
|
}
|
||||||
|
public static void Waiting()
|
||||||
|
{
|
||||||
|
Console.WriteLine("I am waiting for somtething");
|
||||||
|
}
|
||||||
|
public static void Bye()
|
||||||
|
{
|
||||||
|
Console.WriteLine("Bye! Thanks for visiting");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
Messages.Hello();
|
||||||
|
```
|
||||||
|
|
||||||
|
## Object
|
||||||
|
|
||||||
|
Object is an instance of a class
|
||||||
|
can have fields & nethods
|
||||||
|
|
||||||
|
```C#
|
||||||
|
class Human
|
||||||
|
{
|
||||||
|
String name;
|
||||||
|
int age;
|
||||||
|
|
||||||
|
public void Eat()
|
||||||
|
{
|
||||||
|
Console.WriteLine(name + " is eating");
|
||||||
|
}
|
||||||
|
public void Sleep()
|
||||||
|
{
|
||||||
|
Console.WriteLine(name + " is sleeping");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
// in the main class
|
||||||
|
Human human1 = new Human()
|
||||||
|
|
||||||
|
human1.name = "Rick";
|
||||||
|
human1.age = 65;
|
||||||
|
|
||||||
|
human1.Eat();
|
||||||
|
human1.Sleep();
|
||||||
|
|
||||||
|
Human human2 = new Human()
|
||||||
|
|
||||||
|
human1.name = "Morty";
|
||||||
|
human1.age = 16;
|
||||||
|
|
||||||
|
human1.Eat();
|
||||||
|
human1.Sleep();
|
||||||
|
```
|
||||||
|
|
||||||
|
## Construtor
|
||||||
|
|
||||||
|
Constructor is a special method in a class
|
||||||
|
|
||||||
|
```C#
|
||||||
|
class Human
|
||||||
|
{
|
||||||
|
String name;
|
||||||
|
int age;
|
||||||
|
|
||||||
|
public Human(String name, int age){
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Eat()
|
||||||
|
{
|
||||||
|
Console.WriteLine(name + " is eating");
|
||||||
|
}
|
||||||
|
public void Sleep()
|
||||||
|
{
|
||||||
|
Console.WriteLine(name + " is sleeping");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
// in the main class
|
||||||
|
|
||||||
|
Human human1 = new Human("Rick",65)
|
||||||
|
Human human2 = new Human("Morty", 16)
|
||||||
|
|
||||||
|
human1.Eat();
|
||||||
|
human1.Sleep();
|
||||||
|
human2.Eat();
|
||||||
|
human2.Sleep();
|
||||||
|
```
|
||||||
|
|
||||||
|
## static
|
||||||
|
|
||||||
|
static is a modifier to declare a static member, which belongs to the class itself
|
||||||
|
rather than to any specific object
|
||||||
|
|
||||||
|
```C#
|
||||||
|
class Car
|
||||||
|
{
|
||||||
|
String model;
|
||||||
|
public static int numberOfCars;
|
||||||
|
|
||||||
|
public Car(String model)
|
||||||
|
{
|
||||||
|
this.model = model;
|
||||||
|
numberOfCars++;
|
||||||
|
}
|
||||||
|
public static void StartRace()
|
||||||
|
{
|
||||||
|
Console.WriteLine("The race as begun !")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
// in the main class
|
||||||
|
|
||||||
|
Car car1 = new Car("Mustang");
|
||||||
|
Car car2 = new Car("Corvette");
|
||||||
|
Console.WriteLine(Car.numberOfCars);
|
||||||
|
|
||||||
|
Car.StartRace();
|
||||||
|
```
|
||||||
|
|
||||||
|
## overladed constructo
|
||||||
|
|
||||||
|
Technique to create multiple constructors, with a different set of parameters but the same name.
|
||||||
|
name + parameters = signature
|
||||||
|
|
||||||
|
```C#
|
||||||
|
class Pizza
|
||||||
|
{
|
||||||
|
String bread;
|
||||||
|
String sauce;
|
||||||
|
String cheese;
|
||||||
|
String topping;
|
||||||
|
|
||||||
|
public Pizza(String bread,String sauce, String cheese, String topping)
|
||||||
|
{
|
||||||
|
this.bread = bread;
|
||||||
|
this.sauce = sauce;
|
||||||
|
this.cheese = cheese;
|
||||||
|
this.topping = topping;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pizza(String bread,String sauce, String cheese)
|
||||||
|
{
|
||||||
|
this.bread = bread;
|
||||||
|
this.sauce = sauce;
|
||||||
|
this.cheese = cheese;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pizza(String bread,String sauce)
|
||||||
|
{
|
||||||
|
this.bread = bread;
|
||||||
|
this.sauce = sauce;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pizza(String bread)
|
||||||
|
{
|
||||||
|
this.bread = bread;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
// Main
|
||||||
|
|
||||||
|
Pizza pizza = new Pizza("stuffed crust", "red sauce", "mozzarella");
|
||||||
|
Pizza pizza1 = new Pizza("stuffed crust", "red sauce");
|
||||||
|
```
|
||||||
|
|
||||||
|
## inheritance
|
||||||
|
|
||||||
|
1 or more child classes recieving fields, methods, etc. from a common parent
|
||||||
|
|
||||||
|
```C#
|
||||||
|
class Vehicle
|
||||||
|
{
|
||||||
|
public int speed = 0
|
||||||
|
|
||||||
|
public void go()
|
||||||
|
{
|
||||||
|
Console.WriteLine("this vehicle is moving !");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Class Car : Vehicle
|
||||||
|
{
|
||||||
|
public int wheels = 4;
|
||||||
|
}
|
||||||
|
Class Bicycle : Vehicle
|
||||||
|
{
|
||||||
|
public int wheels = 2;
|
||||||
|
}
|
||||||
|
Class Boat : Vehicle
|
||||||
|
{
|
||||||
|
public int wheels = 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
// Main
|
||||||
|
|
||||||
|
Car car = new Car();
|
||||||
|
Bicycle bicycle = new Bicycle();
|
||||||
|
Boat boat = new Boat();
|
||||||
|
|
||||||
|
Console.WriteLine(car.speed());
|
||||||
|
Console.WriteLine(car.wheels());
|
||||||
|
Console.WriteLine(car.go());
|
||||||
|
|
||||||
|
Console.WriteLine(bicycle.speed());
|
||||||
|
Console.WriteLine(bicycle.wheels());
|
||||||
|
Console.WriteLine(bicycle.go());
|
||||||
|
|
||||||
|
Console.WriteLine(boat.speed());
|
||||||
|
Console.WriteLine(boat.wheels());
|
||||||
|
Console.WriteLine(boat.go());
|
||||||
|
```
|
||||||
|
|
||||||
|
## abstract classes
|
||||||
|
|
||||||
|
modifier that indicates missing components or incomplete implemetation
|
||||||
|
|
||||||
|
```C#
|
||||||
|
abstract class Vehicle
|
||||||
|
{
|
||||||
|
public int speed = 0
|
||||||
|
|
||||||
|
public void go()
|
||||||
|
{
|
||||||
|
Console.WriteLine("this vehicle is moving !");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Class Car : Vehicle
|
||||||
|
{
|
||||||
|
public int wheels = 4;
|
||||||
|
int maxSpeed = 500;
|
||||||
|
}
|
||||||
|
Class Bicycle : Vehicle
|
||||||
|
{
|
||||||
|
public int wheels = 2;
|
||||||
|
int maxSpeed = 50;
|
||||||
|
}
|
||||||
|
Class Boat : Vehicle
|
||||||
|
{
|
||||||
|
public int wheels = 0;
|
||||||
|
int maxSpeed = 100;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
// Main
|
||||||
|
Car car = new Car();
|
||||||
|
Bicycle bicycle = new Bicycle();
|
||||||
|
Boat boat = new Boat();
|
||||||
|
Vehicle vehicle new Vehicle();
|
||||||
|
// error can't instanciate
|
||||||
|
```
|
||||||
|
|
||||||
|
## array of object
|
||||||
|
|
||||||
|
```C#
|
||||||
|
Class Car
|
||||||
|
{
|
||||||
|
public String model;
|
||||||
|
public Car(String model)
|
||||||
|
{
|
||||||
|
this.model = model;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
Car[] garage = new Car[3]
|
||||||
|
|
||||||
|
Car car1 = new Car("Mustang");
|
||||||
|
Car car2 = new Car("Corvette");
|
||||||
|
Car car3 = new Car("Lambo");
|
||||||
|
|
||||||
|
garage[0] = car1
|
||||||
|
garage[1] = car2
|
||||||
|
garage[2] = car3
|
||||||
|
|
||||||
|
// or with anonymous object
|
||||||
|
|
||||||
|
Car[] garage = {new Car("Mustang"), new Car("Corvette"), new Car("Lambo")}
|
||||||
|
|
||||||
|
forach(Car car in garage)
|
||||||
|
{
|
||||||
|
Console.WriteLine(car.model());
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
@@ -47,3 +47,16 @@ String fullName = "mrsh online";
|
|||||||
String firstName = fullName.Substring(0,4);
|
String firstName = fullName.Substring(0,4);
|
||||||
String lastName = fullName.Substring(5,6);
|
String lastName = fullName.Substring(5,6);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## String interpolation
|
||||||
|
|
||||||
|
```C#
|
||||||
|
String firstName = "mrsh";
|
||||||
|
String lastName = "Online";
|
||||||
|
int age = 21;
|
||||||
|
|
||||||
|
Console.WriteLine($"Hello {firstName} {lastName}.");
|
||||||
|
|
||||||
|
// include 10 spaces
|
||||||
|
Console.WriteLine($"You are {age, 10} years old.");
|
||||||
|
```
|
||||||
|
|||||||
@@ -85,3 +85,55 @@ if(temp >= 10 && temp <= 25){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## try / catch / exception
|
||||||
|
|
||||||
|
errors that occur during execution
|
||||||
|
|
||||||
|
try = try some code that is considered "dangerous"
|
||||||
|
catch = catches and handles execptions when they occur
|
||||||
|
finally = always executes regardless if exception is caught or not
|
||||||
|
|
||||||
|
```C#
|
||||||
|
double x;
|
||||||
|
double y;
|
||||||
|
double result;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Console.Write("Enter number 1: ");
|
||||||
|
x = Convet.ToDouble(Console.ReadLine());
|
||||||
|
|
||||||
|
Console.Write("Enter number 2: ");
|
||||||
|
y = Convet.ToDouble(Console.ReadLine());
|
||||||
|
|
||||||
|
result = x / y;
|
||||||
|
|
||||||
|
Console.WriteLine("result: " + result);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch(FormatExecption e)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Enter ONLY numbers Please!");
|
||||||
|
}
|
||||||
|
catch(DivideByZeroExecption e)
|
||||||
|
{
|
||||||
|
Console.WriteLine("you can't divide by zero");
|
||||||
|
}
|
||||||
|
catch ( Exeption e)
|
||||||
|
{
|
||||||
|
Console.WriteLine("something went wrong !");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Console.WriteLine("Tanks for visiting");
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Conditional Operator
|
||||||
|
|
||||||
|
// (condition) ? y : x
|
||||||
|
|
||||||
|
```C#
|
||||||
|
double temperature = 20;
|
||||||
|
Console.WriteLine((tempure >= 15) ? "It's warm outside!" : "It's cold outside!"; )
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user