-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
299 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
using System.Reflection; | ||
using Difficulty; | ||
using Difficulty; | ||
|
||
public class Program | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace GenericClass; | ||
public class Car<T1, T2> | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace GenericClass; | ||
public enum EngineEnum | ||
{ | ||
Diesel = 1, | ||
Electric, | ||
Jet | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using GenericClass; | ||
|
||
public class Program | ||
{ | ||
static void Main() | ||
{ | ||
Car<Enum, int> car1 = new Car<Enum, int>(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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace GenericCollection; | ||
|
||
public enum BrandEnum | ||
{ | ||
Toyota = 1, | ||
Honda, | ||
Ferrari | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using GenericCollection; | ||
|
||
public class Program | ||
{ | ||
static void Main() | ||
{ | ||
List<int> list = new List<int> | ||
{ | ||
1, | ||
2, | ||
3, | ||
4 | ||
}; | ||
|
||
foreach (var l in list) | ||
{ | ||
Console.WriteLine(l); | ||
} | ||
|
||
List<Car> carList = new List<Car> | ||
{ | ||
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<int, Car> dict = new Dictionary<int, Car> | ||
{ | ||
{ (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."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 t) where T : class | ||
{ | ||
Console.WriteLine("Info 1: " + t); | ||
} | ||
// Generic method | ||
public void GetInfo<T1, T2>(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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace GenericConstraints; | ||
public interface IEngine | ||
{ | ||
void RunEngine(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |