-
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
12 changed files
with
272 additions
and
1 deletion.
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 |
---|---|---|
@@ -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,32 @@ | ||
public class Program | ||
{ | ||
static void Main() | ||
{ | ||
// two ways to create Array | ||
int[] numbers1 = {1, 2, 3, 4, 5}; | ||
int[] numbers2 = new int[5]; | ||
numbers2[0] = 1; | ||
numbers2[1] = 2; | ||
numbers2[2] = 3; | ||
numbers2[3] = 4; | ||
numbers2[4] = 5; | ||
|
||
// resize array | ||
Array.Resize(ref numbers1, 10); | ||
Console.WriteLine(numbers1.Length); | ||
|
||
// reverse array | ||
Array.Reverse(numbers2); // 5 4 3 2 1 | ||
foreach (var number in numbers2) | ||
{ | ||
Console.WriteLine(number); | ||
} | ||
|
||
// sort array -> ascending | ||
Array.Sort(numbers2); | ||
foreach (var number in numbers2) | ||
{ | ||
Console.WriteLine(number); | ||
} | ||
} | ||
} |
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,16 @@ | ||
using System.Collections; | ||
|
||
public class Program | ||
{ | ||
static void Main() | ||
{ | ||
// ArrayList is not safety | ||
ArrayList list = new ArrayList() { | ||
1, "2", true, 3.4, 123.1f, 111M, new int[] {1, 2, 3} | ||
}; | ||
|
||
// must unbox to access element of ArrayList | ||
int intValue = (int) list[0]; | ||
Console.WriteLine(intValue); | ||
} | ||
} |
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,43 @@ | ||
public class Program | ||
{ | ||
static void Main() | ||
{ | ||
// HashSet is type safety | ||
// HashSet elements must be unique | ||
HashSet<int> hs1 = new(); | ||
hs1.Add(1); | ||
hs1.Add(2); | ||
hs1.Add(2); // this will be discarded | ||
hs1.Add(3); | ||
hs1.Add(4); | ||
PrintHashSet(hs1, nameof(hs1)); | ||
|
||
HashSet<int> hs2 = new HashSet<int>() {3, 4, 5, 6}; | ||
PrintHashSet(hs2, nameof(hs2)); | ||
|
||
HashSet<int> except = new HashSet<int>(hs1); | ||
except.ExceptWith(hs2); | ||
PrintHashSet(except, nameof(except)); | ||
|
||
HashSet<int> union = new HashSet<int>(hs1); | ||
union.UnionWith(hs2); | ||
PrintHashSet(union, nameof(union)); | ||
|
||
HashSet<int> intersect = new HashSet<int>(hs1); | ||
intersect.IntersectWith(hs2); | ||
PrintHashSet(intersect, nameof(intersect)); | ||
|
||
Console.WriteLine($"except is subset of union: {except.IsSubsetOf(union)}"); | ||
Console.WriteLine($"union is superset of except: {union.IsSupersetOf(except)}"); | ||
} | ||
|
||
static void PrintHashSet(HashSet<int> hashSet, string varName) | ||
{ | ||
Console.Write(varName + ": "); | ||
foreach (var hs in hashSet) | ||
{ | ||
Console.Write($"{hs} "); | ||
} | ||
Console.WriteLine(); | ||
} | ||
} |
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,25 @@ | ||
public class Program | ||
{ | ||
static void Main() | ||
{ | ||
LinkedList<int> list = new(); | ||
list.AddLast(2); | ||
list.AddLast(3); | ||
list.AddLast(4); | ||
list.AddFirst(1); | ||
list.AddLast(6); | ||
LinkedListNode<int>? node = list.Find(6); | ||
list.AddBefore(node, 5); | ||
PrintLinkedList(list, nameof(list)); | ||
} | ||
|
||
static void PrintLinkedList(LinkedList<int> list, string varName) | ||
{ | ||
Console.Write(varName + ": "); | ||
foreach (var item in list) | ||
{ | ||
Console.Write($"{item} "); | ||
} | ||
Console.WriteLine(); | ||
} | ||
} |
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,25 @@ | ||
namespace List; | ||
|
||
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,44 @@ | ||
using List; | ||
|
||
public class Program | ||
{ | ||
static void Main() | ||
{ | ||
List<int> listOfNumbers = new List<int>() {2, 4, 1, 9, 3, 5}; | ||
PrintList(listOfNumbers, nameof(listOfNumbers)); | ||
|
||
// Ascending | ||
listOfNumbers.Sort(); | ||
PrintList(listOfNumbers, nameof(listOfNumbers)); | ||
|
||
listOfNumbers.Reverse(); | ||
PrintList(listOfNumbers, nameof(listOfNumbers)); | ||
|
||
List<Car> cars = new List<Car>() {new Car("honda", 200), new Car("ferrari", 400), new Car("toyota", 150)}; | ||
|
||
Console.WriteLine(); | ||
Console.WriteLine("Before sort"); | ||
foreach (var car in cars) | ||
{ | ||
Console.WriteLine($"{car.BrandName} - {car.TopSpeed} kmh"); | ||
} | ||
|
||
Console.WriteLine(); | ||
Console.WriteLine("After sort"); | ||
cars.Sort((car1, car2) => car1.TopSpeed.CompareTo(car2.TopSpeed)); // Sort cars based on TopSpeed | ||
foreach (var car in cars) | ||
{ | ||
Console.WriteLine($"{car.BrandName} - {car.TopSpeed} kmh"); | ||
} | ||
} | ||
|
||
static void PrintList(List<int> list, string varName) | ||
{ | ||
Console.Write(varName + ": "); | ||
foreach (var item in list) | ||
{ | ||
Console.Write($"{item} "); | ||
} | ||
Console.WriteLine(); | ||
} | ||
} |