From f97bd77a5b3cfd2038af133993e0290402c60929 Mon Sep 17 00:00:00 2001 From: Yusuf Praditya <47532266+yusufpraditya@users.noreply.github.com> Date: Tue, 12 Sep 2023 11:55:52 +0700 Subject: [PATCH] Add W2 D2 projects --- FormulatrixBootcamp.sln | 37 +++++++++++++++ W2 D1/Enum/Program.cs | 3 +- W2 D2/Delegate/Delegate.csproj | 10 ++++ W2 D2/Delegate/Program.cs | 18 +++++++ W2 D2/GenericClass/Car.cs | 29 ++++++++++++ W2 D2/GenericClass/EngineEnum.cs | 7 +++ W2 D2/GenericClass/GenericClass.csproj | 8 ++++ W2 D2/GenericClass/Program.cs | 16 +++++++ W2 D2/GenericCollection/BrandEnum.cs | 8 ++++ W2 D2/GenericCollection/Car.cs | 21 +++++++++ .../GenericCollection.csproj | 8 ++++ W2 D2/GenericCollection/Program.cs | 47 +++++++++++++++++++ W2 D2/GenericConstraints/Car.cs | 28 +++++++++++ .../GenericConstraints.csproj | 10 ++++ W2 D2/GenericConstraints/IEngine.cs | 5 ++ W2 D2/GenericConstraints/Program.cs | 13 +++++ .../NonGenericCollection.csproj | 10 ++++ W2 D2/NonGenericCollection/Program.cs | 23 +++++++++ 18 files changed, 299 insertions(+), 2 deletions(-) create mode 100644 W2 D2/Delegate/Delegate.csproj create mode 100644 W2 D2/Delegate/Program.cs create mode 100644 W2 D2/GenericClass/Car.cs create mode 100644 W2 D2/GenericClass/EngineEnum.cs create mode 100644 W2 D2/GenericClass/GenericClass.csproj create mode 100644 W2 D2/GenericClass/Program.cs create mode 100644 W2 D2/GenericCollection/BrandEnum.cs create mode 100644 W2 D2/GenericCollection/Car.cs create mode 100644 W2 D2/GenericCollection/GenericCollection.csproj create mode 100644 W2 D2/GenericCollection/Program.cs create mode 100644 W2 D2/GenericConstraints/Car.cs create mode 100644 W2 D2/GenericConstraints/GenericConstraints.csproj create mode 100644 W2 D2/GenericConstraints/IEngine.cs create mode 100644 W2 D2/GenericConstraints/Program.cs create mode 100644 W2 D2/NonGenericCollection/NonGenericCollection.csproj create mode 100644 W2 D2/NonGenericCollection/Program.cs diff --git a/FormulatrixBootcamp.sln b/FormulatrixBootcamp.sln index 6e1bc7b..7b2df5d 100644 --- a/FormulatrixBootcamp.sln +++ b/FormulatrixBootcamp.sln @@ -59,6 +59,18 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MoreRef", "W2 D1\MoreRef\Mo EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Enum", "W2 D1\Enum\Enum.csproj", "{3B6CBE35-FC4D-4AFC-A6F9-09A7DB9B4378}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "W2 D2", "W2 D2", "{747633C8-D663-4492-9F60-E01A9F09A1CA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GenericConstraints", "W2 D2\GenericConstraints\GenericConstraints.csproj", "{D134C540-432E-414B-82F4-72D084C20224}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GenericCollection", "W2 D2\GenericCollection\GenericCollection.csproj", "{71C8B23F-6C05-4DE7-8ECA-5C4303AB402A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GenericClass", "W2 D2\GenericClass\GenericClass.csproj", "{367138CF-7021-4354-B364-6A674AA8E4D5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NonGenericCollection", "W2 D2\NonGenericCollection\NonGenericCollection.csproj", "{51504E3A-4A98-45D9-B64B-C657E7CC95B9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Delegate", "W2 D2\Delegate\Delegate.csproj", "{8CBF3A9D-D792-4ECC-92A3-0E2782673234}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -157,6 +169,26 @@ Global {3B6CBE35-FC4D-4AFC-A6F9-09A7DB9B4378}.Debug|Any CPU.Build.0 = Debug|Any CPU {3B6CBE35-FC4D-4AFC-A6F9-09A7DB9B4378}.Release|Any CPU.ActiveCfg = Release|Any CPU {3B6CBE35-FC4D-4AFC-A6F9-09A7DB9B4378}.Release|Any CPU.Build.0 = Release|Any CPU + {D134C540-432E-414B-82F4-72D084C20224}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D134C540-432E-414B-82F4-72D084C20224}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D134C540-432E-414B-82F4-72D084C20224}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D134C540-432E-414B-82F4-72D084C20224}.Release|Any CPU.Build.0 = Release|Any CPU + {71C8B23F-6C05-4DE7-8ECA-5C4303AB402A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {71C8B23F-6C05-4DE7-8ECA-5C4303AB402A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {71C8B23F-6C05-4DE7-8ECA-5C4303AB402A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {71C8B23F-6C05-4DE7-8ECA-5C4303AB402A}.Release|Any CPU.Build.0 = Release|Any CPU + {367138CF-7021-4354-B364-6A674AA8E4D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {367138CF-7021-4354-B364-6A674AA8E4D5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {367138CF-7021-4354-B364-6A674AA8E4D5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {367138CF-7021-4354-B364-6A674AA8E4D5}.Release|Any CPU.Build.0 = Release|Any CPU + {51504E3A-4A98-45D9-B64B-C657E7CC95B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {51504E3A-4A98-45D9-B64B-C657E7CC95B9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {51504E3A-4A98-45D9-B64B-C657E7CC95B9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {51504E3A-4A98-45D9-B64B-C657E7CC95B9}.Release|Any CPU.Build.0 = Release|Any CPU + {8CBF3A9D-D792-4ECC-92A3-0E2782673234}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8CBF3A9D-D792-4ECC-92A3-0E2782673234}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8CBF3A9D-D792-4ECC-92A3-0E2782673234}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8CBF3A9D-D792-4ECC-92A3-0E2782673234}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -185,5 +217,10 @@ Global {0D3E0858-A87D-4254-9192-25510F91A630} = {4AA51D7D-BA22-4F16-A2A3-8BD526E4CE19} {C4365CA5-CA1C-47D8-89D8-C03E16A82144} = {4AA51D7D-BA22-4F16-A2A3-8BD526E4CE19} {3B6CBE35-FC4D-4AFC-A6F9-09A7DB9B4378} = {4AA51D7D-BA22-4F16-A2A3-8BD526E4CE19} + {D134C540-432E-414B-82F4-72D084C20224} = {747633C8-D663-4492-9F60-E01A9F09A1CA} + {71C8B23F-6C05-4DE7-8ECA-5C4303AB402A} = {747633C8-D663-4492-9F60-E01A9F09A1CA} + {367138CF-7021-4354-B364-6A674AA8E4D5} = {747633C8-D663-4492-9F60-E01A9F09A1CA} + {51504E3A-4A98-45D9-B64B-C657E7CC95B9} = {747633C8-D663-4492-9F60-E01A9F09A1CA} + {8CBF3A9D-D792-4ECC-92A3-0E2782673234} = {747633C8-D663-4492-9F60-E01A9F09A1CA} EndGlobalSection EndGlobal diff --git a/W2 D1/Enum/Program.cs b/W2 D1/Enum/Program.cs index c2d9786..48b1321 100644 --- a/W2 D1/Enum/Program.cs +++ b/W2 D1/Enum/Program.cs @@ -1,5 +1,4 @@ -using System.Reflection; -using Difficulty; +using Difficulty; public class Program { diff --git a/W2 D2/Delegate/Delegate.csproj b/W2 D2/Delegate/Delegate.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/W2 D2/Delegate/Delegate.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/W2 D2/Delegate/Program.cs b/W2 D2/Delegate/Program.cs new file mode 100644 index 0000000..110ba80 --- /dev/null +++ b/W2 D2/Delegate/Program.cs @@ -0,0 +1,18 @@ +public class Program +{ + // signature: same return type & parameter + public delegate void MyDelegate(string text); + static void Main() + { + MyDelegate d = Print; + + // two ways of calling or invoking delegate + d.Invoke("Hello world"); + d("Hello world"); + + } + static void Print(string text) + { + Console.WriteLine(text); + } +} \ No newline at end of file diff --git a/W2 D2/GenericClass/Car.cs b/W2 D2/GenericClass/Car.cs new file mode 100644 index 0000000..9b04f39 --- /dev/null +++ b/W2 D2/GenericClass/Car.cs @@ -0,0 +1,29 @@ +namespace GenericClass; +public class Car +where T1 : class +where T2 : struct +{ + private T1 _t1; + private T2 _t2; + public Car(T1 t1, T2 t2) + { + _t1 = t1; + _t2 = t2; + } + public T1 GetEngine() + { + return _t1; + } + public T2 GetTopSpeed() + { + return _t2; + } + public void SetEngine(T1 t1) + { + _t1 = t1; + } + public void SetTopSpeed(T2 t2) + { + _t2 = t2; + } +} diff --git a/W2 D2/GenericClass/EngineEnum.cs b/W2 D2/GenericClass/EngineEnum.cs new file mode 100644 index 0000000..0796366 --- /dev/null +++ b/W2 D2/GenericClass/EngineEnum.cs @@ -0,0 +1,7 @@ +namespace GenericClass; +public enum EngineEnum +{ + Diesel = 1, + Electric, + Jet +} diff --git a/W2 D2/GenericClass/GenericClass.csproj b/W2 D2/GenericClass/GenericClass.csproj new file mode 100644 index 0000000..b8e5116 --- /dev/null +++ b/W2 D2/GenericClass/GenericClass.csproj @@ -0,0 +1,8 @@ + + + Exe + net7.0 + enable + enable + + \ No newline at end of file diff --git a/W2 D2/GenericClass/Program.cs b/W2 D2/GenericClass/Program.cs new file mode 100644 index 0000000..c9d902c --- /dev/null +++ b/W2 D2/GenericClass/Program.cs @@ -0,0 +1,16 @@ +using GenericClass; + +public class Program +{ + static void Main() + { + Car car1 = new Car(EngineEnum.Electric, 300); + Console.WriteLine(car1.GetEngine()); + car1.SetEngine(EngineEnum.Jet); + Console.WriteLine(car1.GetEngine()); + + Console.WriteLine(car1.GetTopSpeed()); + car1.SetTopSpeed(900); + Console.WriteLine(car1.GetTopSpeed()); + } +} diff --git a/W2 D2/GenericCollection/BrandEnum.cs b/W2 D2/GenericCollection/BrandEnum.cs new file mode 100644 index 0000000..d74f25d --- /dev/null +++ b/W2 D2/GenericCollection/BrandEnum.cs @@ -0,0 +1,8 @@ +namespace GenericCollection; + +public enum BrandEnum +{ + Toyota = 1, + Honda, + Ferrari +} diff --git a/W2 D2/GenericCollection/Car.cs b/W2 D2/GenericCollection/Car.cs new file mode 100644 index 0000000..4663b5a --- /dev/null +++ b/W2 D2/GenericCollection/Car.cs @@ -0,0 +1,21 @@ +namespace GenericCollection; +public class Car +{ + private string? _brandName; + private int _topSpeed; + public string? BrandName + { + get => _brandName; + set => _brandName = value; + } + public int TopSpeed + { + get => _topSpeed; + set => _topSpeed = value; + } + public Car(string brandName, int topSpeed) + { + _brandName = brandName; + _topSpeed = topSpeed; + } +} diff --git a/W2 D2/GenericCollection/GenericCollection.csproj b/W2 D2/GenericCollection/GenericCollection.csproj new file mode 100644 index 0000000..b8e5116 --- /dev/null +++ b/W2 D2/GenericCollection/GenericCollection.csproj @@ -0,0 +1,8 @@ + + + Exe + net7.0 + enable + enable + + \ No newline at end of file diff --git a/W2 D2/GenericCollection/Program.cs b/W2 D2/GenericCollection/Program.cs new file mode 100644 index 0000000..c7ae4c0 --- /dev/null +++ b/W2 D2/GenericCollection/Program.cs @@ -0,0 +1,47 @@ +using GenericCollection; + +public class Program +{ + static void Main() + { + List list = new List + { + 1, + 2, + 3, + 4 + }; + + foreach (var l in list) + { + Console.WriteLine(l); + } + + List carList = new List + { + new Car("Honda", 300), + new Car("Toyota", 200), + new Car("Ferrari", 500) + }; + carList[0].TopSpeed = 200; + carList[1].BrandName = "Tayo"; + + foreach (var c in carList) + { + Console.WriteLine($"{c.BrandName} has top speed of {c.TopSpeed} kmh."); + } + + Dictionary dict = new Dictionary + { + { (int)BrandEnum.Honda, new Car(BrandEnum.Honda.ToString(), 300) }, + { (int)BrandEnum.Toyota, new Car(BrandEnum.Toyota.ToString(), 200) }, + { (int)BrandEnum.Ferrari, new Car(BrandEnum.Ferrari.ToString(), 500) }, + // { (int)BrandEnum.Ferrari, new Car(BrandEnum.Ferrari.ToString(), 500) } // Exception, An item with the same key has already been added. Key: 3 + }; + + foreach (var d in dict) + { + Console.WriteLine($"{d.Value.BrandName} has top speed of {d.Value.TopSpeed} kmh."); + } + } +} \ No newline at end of file diff --git a/W2 D2/GenericConstraints/Car.cs b/W2 D2/GenericConstraints/Car.cs new file mode 100644 index 0000000..0d73ef0 --- /dev/null +++ b/W2 D2/GenericConstraints/Car.cs @@ -0,0 +1,28 @@ +namespace GenericConstraints; +public class Car : IEngine +{ + private string? _carName; + public string? CarName + { + get => _carName; + set => _carName = value; + } + + // Generic method + public void GetInfo(T t) where T : class + { + Console.WriteLine("Info 1: " + t); + } + // Generic method + public void GetInfo(T1 t1, T2 t2) + where T1 : class + where T2 : struct + { + Console.WriteLine("Info 1: " + t1); + Console.WriteLine("Info 2: " + t2); + } + public void RunEngine() + { + Console.WriteLine("Engine is running"); + } +} diff --git a/W2 D2/GenericConstraints/GenericConstraints.csproj b/W2 D2/GenericConstraints/GenericConstraints.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/W2 D2/GenericConstraints/GenericConstraints.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/W2 D2/GenericConstraints/IEngine.cs b/W2 D2/GenericConstraints/IEngine.cs new file mode 100644 index 0000000..aa8254c --- /dev/null +++ b/W2 D2/GenericConstraints/IEngine.cs @@ -0,0 +1,5 @@ +namespace GenericConstraints; +public interface IEngine +{ + void RunEngine(); +} diff --git a/W2 D2/GenericConstraints/Program.cs b/W2 D2/GenericConstraints/Program.cs new file mode 100644 index 0000000..b6b3351 --- /dev/null +++ b/W2 D2/GenericConstraints/Program.cs @@ -0,0 +1,13 @@ +using GenericConstraints; + +public static class Program +{ + static void Main() + { + Car car = new(); + car.CarName = "Toyota"; + car.GetInfo(car.CarName); + car.GetInfo("Honda", 200); + // car.GetInfo(200, "Honda"); // <- error, first param should be class & second param should be struct + } +} \ No newline at end of file diff --git a/W2 D2/NonGenericCollection/NonGenericCollection.csproj b/W2 D2/NonGenericCollection/NonGenericCollection.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/W2 D2/NonGenericCollection/NonGenericCollection.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/W2 D2/NonGenericCollection/Program.cs b/W2 D2/NonGenericCollection/Program.cs new file mode 100644 index 0000000..7f12eb9 --- /dev/null +++ b/W2 D2/NonGenericCollection/Program.cs @@ -0,0 +1,23 @@ +using System.Collections; + +public class Program +{ + static void Main() + { + // ArrayList accepts all objects, it is not safety collection. + ArrayList arrList = new ArrayList(); + arrList.Add(new Class1()); + arrList.Add(3); + arrList.Add("abcdef"); + ((Class1)arrList[0]).Method1(); + // ((Class1)arrList[1]).Method1(); // Unable to cast object of type 'System.Int32' to type 'Class1'. + } +} + +public class Class1 +{ + public void Method1() + { + Console.WriteLine("Hi"); + } +} \ No newline at end of file